Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub Confirm_Selection_Click()
  2. Application.ScreenUpdating = False
  3. Application.Calculation = xlCalculationManual
  4. Application.EnableEvents = False
  5.  
  6.  
  7.  
  8. 'Application.ScreenUpdating = True
  9. 'Application.Calculation = xlCalculationAutomatic
  10. 'Application.EnableEvents = True
  11.  
  12.  
  13.     Dim j As Long 'Created Row Number
  14.    Dim t As Integer
  15.     Dim currentRowCount As Long 'Current Table Row Numer
  16.    Dim tbl As ListObject
  17.     Dim lastRow As Long
  18.     Dim caseMan As String
  19.     Dim caseBrand As String
  20.     Dim caseSubBrand As String
  21.     Dim caseFlavour As String
  22.    
  23.     Set tbl = Sheet1.ListObjects("CMR")
  24.     currentRowCount = tbl.ListRows.Count
  25.     t = 1
  26.    
  27.                
  28.     'Creation of ranges
  29.    AE = Sheet3.Range("B" & Sheet3.Rows.Count).End(xlUp).Row
  30.     BE = Sheet3.Range("J" & Sheet3.Rows.Count).End(xlUp).Row
  31.     CH = Sheet3.Range("R" & Sheet3.Rows.Count).End(xlUp).Row
  32.     DE03 = Sheet3.Range("Z" & Sheet3.Rows.Count).End(xlUp).Row
  33.     DE04 = Sheet3.Range("AH" & Sheet3.Rows.Count).End(xlUp).Row
  34.     DE05 = Sheet3.Range("AP" & Sheet3.Rows.Count).End(xlUp).Row
  35.     DE06 = Sheet3.Range("AX" & Sheet3.Rows.Count).End(xlUp).Row
  36.     DE07 = Sheet3.Range("BF" & Sheet3.Rows.Count).End(xlUp).Row
  37.     DE08 = Sheet3.Range("GN" & Sheet3.Rows.Count).End(xlUp).Row
  38.     ES01 = Sheet3.Range("BV" & Sheet3.Rows.Count).End(xlUp).Row
  39.     ES03 = Sheet3.Range("CD" & Sheet3.Rows.Count).End(xlUp).Row
  40.     ES04 = Sheet3.Range("CL" & Sheet3.Rows.Count).End(xlUp).Row
  41.     FR = Sheet3.Range("CT" & Sheet3.Rows.Count).End(xlUp).Row
  42.     GR = Sheet3.Range("DB" & Sheet3.Rows.Count).End(xlUp).Row
  43.     IE = Sheet3.Range("DJ" & Sheet3.Rows.Count).End(xlUp).Row
  44.     KW = Sheet3.Range("DR" & Sheet3.Rows.Count).End(xlUp).Row
  45.     NL = Sheet3.Range("DZ" & Sheet3.Rows.Count).End(xlUp).Row
  46.     PT = Sheet3.Range("EH" & Sheet3.Rows.Count).End(xlUp).Row
  47.     SA = Sheet3.Range("EP" & Sheet3.Rows.Count).End(xlUp).Row
  48.     UT01 = Sheet3.Range("EX" & Sheet3.Rows.Count).End(xlUp).Row
  49.     UT02 = Sheet3.Range("FF" & Sheet3.Rows.Count).End(xlUp).Row
  50.     UK03 = Sheet3.Range("FN" & Sheet3.Rows.Count).End(xlUp).Row
  51.     UK04 = Sheet3.Range("FV" & Sheet3.Rows.Count).End(xlUp).Row
  52.     UK05 = Sheet3.Range("GD" & Sheet3.Rows.Count).End(xlUp).Row
  53.     UK06 = Sheet3.Range("GL" & Sheet3.Rows.Count).End(xlUp).Row
  54.     UK07 = Sheet3.Range("GT" & Sheet3.Rows.Count).End(xlUp).Row
  55.     UK08 = Sheet3.Range("HB" & Sheet3.Rows.Count).End(xlUp).Row
  56.     UK09 = Sheet3.Range("HJ" & Sheet3.Rows.Count).End(xlUp).Row
  57.     UK10 = Sheet3.Range("HR" & Sheet3.Rows.Count).End(xlUp).Row
  58.    
  59.     countryLastRow = Array(AE, BE, CH, DE03, DE04, DE05, DE06, DE07, DE08, ES01, ES03, ES04, FR, GR, IE, KW, NL, PT, SA, UT01, UT02, UK03, UK04, UK05, UK06, UK07, UK08, UK09, UK10)
  60.     countryRanges = Array(2, 10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90, 98, 106, 114, 122, 130, 138, 146, 154, 162, 170, 178, 186, 194, 202, 210, 218, 226)
  61.    
  62.     'Create Last Row in CMR sheet
  63.    lastRow = WorksheetFunction.Max(countryLastRow)
  64.    
  65.     'Create the number of rows needed
  66.    countryRowsBegin = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  67.    
  68.     'Creation of case selection
  69.    If Sheet1.Range("C3") = "ALL MANUFACTURERS" Then
  70.         caseMan = "All Manufacturers"
  71.     Else
  72.         caseMan = "Chosenm"
  73.     End If
  74.    
  75.     If Sheet1.Range("C4") = "ALL BRANDS" Then
  76.         caseBrand = "All Brands"
  77.     Else
  78.         caseBrand = "Chosenb"
  79.     End If
  80.     If Sheet1.Range("C5") = "ALL SUB BRANDS" Then
  81.         caseSubBrand = "All Sub Brands"
  82.     Else
  83.         caseSubBrand = "Chosensb"
  84.     End If
  85.     If Sheet1.Range("C6") = "ALL FLAVOURS" Then
  86.         caseFlavour = "All Flavours"
  87.     Else
  88.         caseFlavour = "Chosenfl"
  89.     End If
  90.  
  91.     If Sheet1.ManuBox.Value = "" Or Sheet1.BrandBox.Value = "" Or Sheet1.SubBox.Value = "" Or Sheet1.FlavourBox.Value = "" Then
  92.         MsgBox ("Please fill out all selections")
  93.         Exit Sub
  94.     End If
  95.  
  96.     Select Case caseMan
  97.         Case "All Manufacturers"
  98.             Select Case caseBrand
  99.                 Case "All Brands"
  100.                     Select Case caseSubBrand
  101.                         Case "All Sub Brands"
  102.                             Select Case caseFlavour
  103.                                 Case "All Flavours"
  104.                                 'Depends on number of manufacturers
  105.                                
  106.                                     neededRowCount = lastRow - 3
  107.                            
  108.                                     'Adds table rows to match number in j
  109.                                    Do While neededRowCount > currentRowCount
  110.                                         tbl.ListRows.Add AlwaysInsert:=True
  111.                                         currentRowCount = currentRowCount + 1
  112.                                     Loop
  113.                            
  114.                                     'Deletes table rows to match number in j
  115.                                    Do While currentRowCount > neededRowCount
  116.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  117.                                             tbl.ListRows(currentRowCount).Delete
  118.                                         Next currentRowCount
  119.                                     Loop
  120.                            
  121.                                     'When table rows matches j this will tell you to when you can add data
  122.                                    If currentRowCount = neededRowCount Then
  123.                                         MsgBox ("The Data will be added when you press OK")
  124.                                     End If
  125.                                    
  126.                                         For k = 0 To 28
  127.                                             For v = 1 To currentRowCount
  128.                                                 If IsEmpty(Sheet3.Cells(v + 3, countryRanges(k))) = False Then
  129.                                                     Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(v, countryRanges(k) - 1)          'Manufacturer
  130.                                                    Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(v, countryRanges(k))          'Brand
  131.                                                    Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(v, countryRanges(k) + 1)      'SubBrand
  132.                                                    Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(v, countryRanges(k) + 2)      'Flavour
  133.                                                    Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(v, countryRanges(k) + 3)      'SKU
  134.                                                    Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(v, countryRanges(k) + 4)      'Count
  135.                                                    Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(v, countryRanges(k) + 5)      'Size
  136.                                                End If
  137.                                             Next v
  138.                                         Next k
  139.                                        
  140.                                         Application.ScreenUpdating = True
  141.                                         Application.Calculation = xlCalculationAutomatic
  142.                                         Application.EnableEvents = True
  143.  
  144.                                 Case "Chosenfl"
  145.                                 'Depends on number of manufacturers with the chosen flavour
  146.                                
  147.                                     For v = 4 To lastRow
  148.                                         For i = 0 To 28
  149.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIf(Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  150.                                         Next i
  151.                                     Next v
  152.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  153.                            
  154.                                     'Adds table rows to match number in j
  155.                                    Do While neededRowCount > currentRowCount
  156.                                         tbl.ListRows.Add AlwaysInsert:=True
  157.                                         currentRowCount = currentRowCount + 1
  158.                                     Loop
  159.                            
  160.                                     'Deletes table rows to match number in j
  161.                                    Do While currentRowCount > neededRowCount
  162.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  163.                                             tbl.ListRows(currentRowCount).Delete
  164.                                         Next currentRowCount
  165.                                     Loop
  166.                            
  167.                                     'When table rows matches j this will tell you to when you can add data
  168.                                    If currentRowCount = neededRowCount Then
  169.                                         MsgBox ("The Data will be added when you press OK")
  170.                                     End If
  171.                                    
  172.                                         For k = 0 To 28
  173.                                             For v = 1 To countryLastRow(k)
  174.                                                 If Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  175.                                                     For p = t To currentRowCount
  176.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  177.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  178.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  179.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  180.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  181.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  182.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  183.                                                        Exit For
  184.                                                     Next p
  185.                                                     t = t + 1
  186.                                                 End If
  187.                                             Next v
  188.                                             t = 1
  189.                                         Next k
  190.                                        
  191.                                         Application.ScreenUpdating = True
  192.                                         Application.Calculation = xlCalculationAutomatic
  193.                                         Application.EnableEvents = True
  194.                                        
  195.                                
  196.                             End Select
  197.                            
  198.                         Case "Chosensb"
  199.                             Select Case caseFlavour
  200.                                 Case "All Flavours"
  201.                                 'Depends on number of Sub Brands Chosen
  202.                                
  203.                                     For v = 4 To lastRow
  204.                                         For i = 0 To 28
  205.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIf(Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"))
  206.                                         Next i
  207.                                     Next v
  208.                                    
  209.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  210.                            
  211.                                     'Adds table rows to match number in j
  212.                                    Do While neededRowCount > currentRowCount
  213.                                         tbl.ListRows.Add AlwaysInsert:=True
  214.                                         currentRowCount = currentRowCount + 1
  215.                                     Loop
  216.                            
  217.                                     'Deletes table rows to match number in j
  218.                                    Do While currentRowCount > neededRowCount
  219.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  220.                                             tbl.ListRows(currentRowCount).Delete
  221.                                         Next currentRowCount
  222.                                     Loop
  223.                            
  224.                                     'When table rows matches j this will tell you to when you can add data
  225.                                    If currentRowCount = neededRowCount Then
  226.                                         MsgBox ("The Data will be added when you press OK")
  227.                                     End If
  228.                                
  229.                                         For k = 0 To 28
  230.                                             For v = 1 To countryLastRow(k)
  231.                                                 If Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") Then
  232.                                                     For p = t To currentRowCount
  233.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  234.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  235.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  236.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  237.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  238.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  239.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  240.                                                        Exit For
  241.                                                     Next p
  242.                                                     t = t + 1
  243.                                                 End If
  244.                                             Next v
  245.                                             t = 1
  246.                                         Next k
  247.                                        
  248.                                         Application.ScreenUpdating = True
  249.                                         Application.Calculation = xlCalculationAutomatic
  250.                                         Application.EnableEvents = True
  251.                                
  252.                                 Case "Chosenfl"
  253.                                 'Depends on number of Sub Brands with chosen flavour
  254.                                
  255.                                     For v = 4 To lastRow
  256.                                         For i = 0 To 28
  257.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"), Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  258.                                         Next i
  259.                                     Next v
  260.                                    
  261.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  262.                            
  263.                                     'Adds table rows to match number in j
  264.                                    Do While neededRowCount > currentRowCount
  265.                                         tbl.ListRows.Add AlwaysInsert:=True
  266.                                         currentRowCount = currentRowCount + 1
  267.                                     Loop
  268.                            
  269.                                     'Deletes table rows to match number in j
  270.                                    Do While currentRowCount > neededRowCount
  271.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  272.                                             tbl.ListRows(currentRowCount).Delete
  273.                                         Next currentRowCount
  274.                                     Loop
  275.                            
  276.                                     'When table rows matches j this will tell you to when you can add data
  277.                                    If currentRowCount = neededRowCount Then
  278.                                         MsgBox ("The Data will be added when you press OK")
  279.                                     End If
  280.                                
  281.                                         For k = 0 To 28
  282.                                             For v = 1 To countryLastRow(k)
  283.                                                 If Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") And Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  284.                                                     For p = t To currentRowCount
  285.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  286.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  287.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  288.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  289.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  290.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  291.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  292.                                                        Exit For
  293.                                                     Next p
  294.                                                     t = t + 1
  295.                                                 End If
  296.                                             Next v
  297.                                             t = 1
  298.                                         Next k
  299.                                        
  300.                                         Application.ScreenUpdating = True
  301.                                         Application.Calculation = xlCalculationAutomatic
  302.                                         Application.EnableEvents = True
  303.                                        
  304.                             End Select
  305.                     End Select
  306.                    
  307.                    
  308.                 Case "Chosenb"
  309.                     Select Case caseSubBrand
  310.                         Case "All Sub Brands"
  311.                             Select Case caseFlavour
  312.                                 Case "All Flavours"
  313.                                 'Depends on number of Brands chosen
  314.                                
  315.                                     For v = 4 To lastRow
  316.                                         For i = 0 To 28
  317.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIf(Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"))
  318.                                         Next i
  319.                                     Next v
  320.                                    
  321.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  322.                            
  323.                                     'Adds table rows to match number in j
  324.                                    Do While neededRowCount > currentRowCount
  325.                                         tbl.ListRows.Add AlwaysInsert:=True
  326.                                         currentRowCount = currentRowCount + 1
  327.                                     Loop
  328.                            
  329.                                     'Deletes table rows to match number in j
  330.                                    Do While currentRowCount > neededRowCount
  331.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  332.                                             tbl.ListRows(currentRowCount).Delete
  333.                                         Next currentRowCount
  334.                                     Loop
  335.                            
  336.                                     'When table rows matches j this will tell you to when you can add data
  337.                                    If currentRowCount = neededRowCount Then
  338.                                         MsgBox ("The Data will be added when you press OK")
  339.                                     End If
  340.                                
  341.                                         For k = 0 To 28
  342.                                             For v = 1 To countryLastRow(k)
  343.                                                 If Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") Then
  344.                                                     For p = t To currentRowCount
  345.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  346.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  347.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  348.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  349.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  350.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  351.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  352.                                                        Exit For
  353.                                                     Next p
  354.                                                     t = t + 1
  355.                                                 End If
  356.                                             Next v
  357.                                             t = 1
  358.                                         Next k
  359.                                        
  360.                                         Application.ScreenUpdating = True
  361.                                         Application.Calculation = xlCalculationAutomatic
  362.                                         Application.EnableEvents = True
  363.                                
  364.                                
  365.                                
  366.                                
  367.                                 Case "Chosenfl"
  368.                                 'Depends on number of Brands chosen with chosen Flavour
  369.                                
  370.                                     For v = 4 To lastRow
  371.                                         For i = 0 To 28
  372.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"), Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  373.                                         Next i
  374.                                     Next v
  375.                                    
  376.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  377.                            
  378.                                     'Adds table rows to match number in j
  379.                                    Do While neededRowCount > currentRowCount
  380.                                         tbl.ListRows.Add AlwaysInsert:=True
  381.                                         currentRowCount = currentRowCount + 1
  382.                                     Loop
  383.                            
  384.                                     'Deletes table rows to match number in j
  385.                                    Do While currentRowCount > neededRowCount
  386.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  387.                                             tbl.ListRows(currentRowCount).Delete
  388.                                         Next currentRowCount
  389.                                     Loop
  390.                            
  391.                                     'When table rows matches j this will tell you to when you can add data
  392.                                    If currentRowCount = neededRowCount Then
  393.                                         MsgBox ("The Data will be added when you press OK")
  394.                                     End If
  395.                                
  396.                                         For k = 0 To 28
  397.                                             For v = 1 To countryLastRow(k)
  398.                                                 If Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") And Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  399.                                                     For p = t To currentRowCount
  400.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  401.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  402.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  403.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  404.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  405.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  406.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  407.                                                        Exit For
  408.                                                     Next p
  409.                                                     t = t + 1
  410.                                                 End If
  411.                                             Next v
  412.                                             t = 1
  413.                                         Next k
  414.                                        
  415.                                         Application.ScreenUpdating = True
  416.                                         Application.Calculation = xlCalculationAutomatic
  417.                                         Application.EnableEvents = True
  418.                                
  419.                                
  420.                                
  421.                                
  422.                             End Select
  423.                            
  424.                         Case "Chosensb"
  425.                             Select Case caseFlavour
  426.                                 Case "All Flavours"
  427.                                 'Depends on number of Brands chosen with chosen Sub Brand
  428.                                
  429.                                     For v = 4 To lastRow
  430.                                         For i = 0 To 28
  431.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"), Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"))
  432.                                         Next i
  433.                                     Next v
  434.                                    
  435.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  436.                            
  437.                                     'Adds table rows to match number in j
  438.                                    Do While neededRowCount > currentRowCount
  439.                                         tbl.ListRows.Add AlwaysInsert:=True
  440.                                         currentRowCount = currentRowCount + 1
  441.                                     Loop
  442.                            
  443.                                     'Deletes table rows to match number in j
  444.                                    Do While currentRowCount > neededRowCount
  445.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  446.                                             tbl.ListRows(currentRowCount).Delete
  447.                                         Next currentRowCount
  448.                                     Loop
  449.                            
  450.                                     'When table rows matches j this will tell you to when you can add data
  451.                                    If currentRowCount = neededRowCount Then
  452.                                         MsgBox ("The Data will be added when you press OK")
  453.                                     End If
  454.                                                        
  455.                                         For k = 0 To 28
  456.                                             For v = 1 To countryLastRow(k)
  457.                                                 If Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") And Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") Then
  458.                                                     For p = t To currentRowCount
  459.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  460.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  461.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  462.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  463.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  464.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  465.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  466.                                                        Exit For
  467.                                                     Next p
  468.                                                     t = t + 1
  469.                                                 End If
  470.                                             Next v
  471.                                             t = 1
  472.                                         Next k
  473.                                        
  474.                                         Application.ScreenUpdating = True
  475.                                         Application.Calculation = xlCalculationAutomatic
  476.                                         Application.EnableEvents = True
  477.                                        
  478.                                        
  479.                                
  480.                                 Case "Chosenfl"
  481.                                 'Depends on number of Brands chosen with chosen Sub Brand with chosen Flavour
  482.                                
  483.                                     For v = 4 To lastRow
  484.                                         For i = 0 To 28
  485.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"), Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"), Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  486.                                         Next i
  487.                                     Next v
  488.                                    
  489.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  490.                            
  491.                                     'Adds table rows to match number in j
  492.                                    Do While neededRowCount > currentRowCount
  493.                                         tbl.ListRows.Add AlwaysInsert:=True
  494.                                         currentRowCount = currentRowCount + 1
  495.                                     Loop
  496.                            
  497.                                     'Deletes table rows to match number in j
  498.                                    Do While currentRowCount > neededRowCount
  499.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  500.                                             tbl.ListRows(currentRowCount).Delete
  501.                                         Next currentRowCount
  502.                                     Loop
  503.                            
  504.                                     'When table rows matches j this will tell you to when you can add data
  505.                                    If currentRowCount = neededRowCount Then
  506.                                         MsgBox ("The Data will be added when you press OK")
  507.                                     End If
  508.                                    
  509.                                         For k = 0 To 28
  510.                                             For v = 1 To countryLastRow(k)
  511.                                                 If Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") And Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") And Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  512.                                                     For p = t To currentRowCount
  513.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  514.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  515.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  516.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  517.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  518.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  519.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  520.                                                        Exit For
  521.                                                     Next p
  522.                                                     t = t + 1
  523.                                                 End If
  524.                                             Next v
  525.                                             t = 1
  526.                                         Next k
  527.                                        
  528.                                         Application.ScreenUpdating = True
  529.                                         Application.Calculation = xlCalculationAutomatic
  530.                                         Application.EnableEvents = True
  531.                                    
  532.                                    
  533.                                    
  534.                                
  535.                             End Select
  536.                     End Select
  537.             End Select
  538.        
  539.    
  540.         Case "Chosenm"
  541.             Select Case caseBrand
  542.                 Case "All Brands"
  543.                     Select Case caseSubBrand
  544.                         Case "All Sub Brands"
  545.                             Select Case caseFlavour
  546.                                 Case "All Flavours"
  547.                                 'Depends on number of Manufacturers Chosen
  548.                                
  549.                                     For v = 4 To lastRow
  550.                                         For i = 0 To 28
  551.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIf(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"))
  552.                                         Next i
  553.                                     Next v
  554.                        
  555.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  556.                        
  557.                                     'Adds table rows to match number in j
  558.                                    Do While neededRowCount > currentRowCount
  559.                                         tbl.ListRows.Add AlwaysInsert:=True
  560.                                         currentRowCount = currentRowCount + 1
  561.                                     Loop
  562.                        
  563.                                     'Deletes table rows to match number in j
  564.                                    Do While currentRowCount > neededRowCount
  565.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  566.                                             tbl.ListRows(currentRowCount).Delete
  567.                                         Next currentRowCount
  568.                                     Loop
  569.                        
  570.                                     'When table rows matches j this will tell you to when you can add data
  571.                                    If currentRowCount = neededRowCount Then
  572.                                         MsgBox ("The Data will be added when you press OK")
  573.                                     End If
  574.                                    
  575.                                         For k = 0 To 28
  576.                                             For v = 1 To countryLastRow(k)
  577.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") Then
  578.                                                     For p = t To currentRowCount
  579.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  580.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  581.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  582.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  583.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  584.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  585.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  586.                                                        Exit For
  587.                                                     Next p
  588.                                                     t = t + 1
  589.                                                 End If
  590.                                             Next v
  591.                                             t = 1
  592.                                         Next k
  593.                                        
  594.                                         Application.ScreenUpdating = True
  595.                                         Application.Calculation = xlCalculationAutomatic
  596.                                         Application.EnableEvents = True
  597.                                
  598.  
  599.                                 Case "Chosenfl"
  600.                                 'Depends on number of Manufacturers chosen with chosen Flavour
  601.                                
  602.                                     For v = 4 To lastRow
  603.                                         For i = 0 To 28
  604.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"), Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  605.                                         Next i
  606.                                     Next v
  607.                        
  608.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  609.                        
  610.                                     'Adds table rows to match number in j
  611.                                    Do While neededRowCount > currentRowCount
  612.                                         tbl.ListRows.Add AlwaysInsert:=True
  613.                                         currentRowCount = currentRowCount + 1
  614.                                     Loop
  615.                        
  616.                                     'Deletes table rows to match number in j
  617.                                    Do While currentRowCount > neededRowCount
  618.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  619.                                             tbl.ListRows(currentRowCount).Delete
  620.                                         Next currentRowCount
  621.                                     Loop
  622.                        
  623.                                     'When table rows matches j this will tell you to when you can add data
  624.                                    If currentRowCount = neededRowCount Then
  625.                                         MsgBox ("The Data will be added when you press OK")
  626.                                     End If
  627.                                    
  628.                                         For k = 0 To 28
  629.                                             For v = 1 To countryLastRow(k)
  630.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") And Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  631.                                                     For p = t To currentRowCount
  632.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  633.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  634.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  635.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  636.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  637.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  638.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  639.                                                        Exit For
  640.                                                     Next p
  641.                                                     t = t + 1
  642.                                                 End If
  643.                                             Next v
  644.                                             t = 1
  645.                                         Next k
  646.                                        
  647.                                         Application.ScreenUpdating = True
  648.                                         Application.Calculation = xlCalculationAutomatic
  649.                                         Application.EnableEvents = True
  650.  
  651.                             End Select
  652.                         Case "Chosensb"
  653.                             Select Case caseFlavour
  654.                                 Case "All Flavours"
  655.                                 'Depends on number of Manufacturers with chosen Sub Brand
  656.                                
  657.                                     For v = 4 To lastRow
  658.                                         For i = 0 To 28
  659.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"), Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"))
  660.                                         Next i
  661.                                     Next v
  662.                        
  663.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  664.                        
  665.                                     'Adds table rows to match number in j
  666.                                    Do While neededRowCount > currentRowCount
  667.                                         tbl.ListRows.Add AlwaysInsert:=True
  668.                                         currentRowCount = currentRowCount + 1
  669.                                     Loop
  670.                        
  671.                                     'Deletes table rows to match number in j
  672.                                    Do While currentRowCount > neededRowCount
  673.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  674.                                             tbl.ListRows(currentRowCount).Delete
  675.                                         Next currentRowCount
  676.                                     Loop
  677.                        
  678.                                     'When table rows matches j this will tell you to when you can add data
  679.                                    If currentRowCount = neededRowCount Then
  680.                                         MsgBox ("The Data will be added when you press OK")
  681.                                     End If
  682.                                    
  683.                                         For k = 0 To 28
  684.                                             For v = 1 To countryLastRow(k)
  685.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") And Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") Then
  686.                                                     For p = t To currentRowCount
  687.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  688.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  689.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  690.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  691.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  692.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  693.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  694.                                                        Exit For
  695.                                                     Next p
  696.                                                     t = t + 1
  697.                                                 End If
  698.                                             Next v
  699.                                             t = 1
  700.                                         Next k
  701.                                        
  702.                                         Application.ScreenUpdating = True
  703.                                         Application.Calculation = xlCalculationAutomatic
  704.                                         Application.EnableEvents = True
  705.                                    
  706.                                
  707.                                 Case "Chosenfl"
  708.                                 'Depends on number of Manufacturers with chosen Sub Brand and chosen Flavour
  709.                                
  710.                                     For v = 4 To lastRow
  711.                                         For i = 0 To 28
  712.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"), Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"), Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  713.                                         Next i
  714.                                     Next v
  715.                        
  716.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  717.                        
  718.                                     'Adds table rows to match number in j
  719.                                    Do While neededRowCount > currentRowCount
  720.                                         tbl.ListRows.Add AlwaysInsert:=True
  721.                                         currentRowCount = currentRowCount + 1
  722.                                     Loop
  723.                        
  724.                                     'Deletes table rows to match number in j
  725.                                    Do While currentRowCount > neededRowCount
  726.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  727.                                             tbl.ListRows(currentRowCount).Delete
  728.                                         Next currentRowCount
  729.                                     Loop
  730.                        
  731.                                     'When table rows matches j this will tell you to when you can add data
  732.                                    If currentRowCount = neededRowCount Then
  733.                                         MsgBox ("The Data will be added when you press OK")
  734.                                     End If
  735.                                    
  736.                                         For k = 0 To 28
  737.                                             For v = 1 To countryLastRow(k)
  738.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") And Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") And Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  739.                                                     For p = t To currentRowCount
  740.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  741.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  742.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  743.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  744.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  745.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  746.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  747.                                                        Exit For
  748.                                                     Next p
  749.                                                     t = t + 1
  750.                                                 End If
  751.                                             Next v
  752.                                             t = 1
  753.                                         Next k
  754.                                        
  755.                                         Application.ScreenUpdating = True
  756.                                         Application.Calculation = xlCalculationAutomatic
  757.                                         Application.EnableEvents = True
  758.                                
  759.                             End Select
  760.                     End Select
  761.                    
  762.                 Case "Chosenb"
  763.                     Select Case caseSubBrand
  764.                         Case "All Sub Brands"
  765.                             Select Case caseFlavour
  766.                                 Case "All Flavours"
  767.                                 'Depends on number of Manufacturers with chosen Brand
  768.                                
  769.                                     For v = 4 To lastRow
  770.                                         For i = 0 To 28
  771.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"), Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"))
  772.                                         Next i
  773.                                     Next v
  774.                        
  775.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  776.                        
  777.                                     'Adds table rows to match number in j
  778.                                    Do While neededRowCount > currentRowCount
  779.                                         tbl.ListRows.Add AlwaysInsert:=True
  780.                                         currentRowCount = currentRowCount + 1
  781.                                     Loop
  782.                        
  783.                                     'Deletes table rows to match number in j
  784.                                    Do While currentRowCount > neededRowCount
  785.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  786.                                             tbl.ListRows(currentRowCount).Delete
  787.                                         Next currentRowCount
  788.                                     Loop
  789.                        
  790.                                     'When table rows matches j this will tell you to when you can add data
  791.                                    If currentRowCount = neededRowCount Then
  792.                                         MsgBox ("The Data will be added when you press OK")
  793.                                     End If
  794.                                    
  795.                                         For k = 0 To 28
  796.                                             For v = 1 To countryLastRow(k)
  797.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") And Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") Then
  798.                                                     For p = t To currentRowCount
  799.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  800.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  801.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  802.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  803.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  804.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  805.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  806.                                                        Exit For
  807.                                                     Next p
  808.                                                     t = t + 1
  809.                                                 End If
  810.                                             Next v
  811.                                             t = 1
  812.                                         Next k
  813.                                        
  814.                                         Application.ScreenUpdating = True
  815.                                         Application.Calculation = xlCalculationAutomatic
  816.                                         Application.EnableEvents = True
  817.                                
  818.                                 Case "Chosenfl"
  819.                                 'Depends on number of Manufacturers with chosen Brand and chosen Flavour
  820.                                
  821.                                     For v = 4 To lastRow
  822.                                         For i = 0 To 28
  823.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"), Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"), Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  824.                                         Next i
  825.                                     Next v
  826.                        
  827.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  828.                        
  829.                                     'Adds table rows to match number in j
  830.                                    Do While neededRowCount > currentRowCount
  831.                                         tbl.ListRows.Add AlwaysInsert:=True
  832.                                         currentRowCount = currentRowCount + 1
  833.                                     Loop
  834.                        
  835.                                     'Deletes table rows to match number in j
  836.                                    Do While currentRowCount > neededRowCount
  837.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  838.                                             tbl.ListRows(currentRowCount).Delete
  839.                                         Next currentRowCount
  840.                                     Loop
  841.                        
  842.                                     'When table rows matches j this will tell you to when you can add data
  843.                                    If currentRowCount = neededRowCount Then
  844.                                         MsgBox ("The Data will be added when you press OK")
  845.                                     End If
  846.                                    
  847.                                         For k = 0 To 28
  848.                                             For v = 1 To countryLastRow(k)
  849.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") And Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") And Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  850.                                                     For p = t To currentRowCount
  851.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  852.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  853.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  854.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  855.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  856.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  857.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  858.                                                        Exit For
  859.                                                     Next p
  860.                                                     t = t + 1
  861.                                                 End If
  862.                                             Next v
  863.                                             t = 1
  864.                                         Next k
  865.                                        
  866.                                         Application.ScreenUpdating = True
  867.                                         Application.Calculation = xlCalculationAutomatic
  868.                                         Application.EnableEvents = True
  869.                                
  870.                             End Select
  871.                         Case "Chosensb"
  872.                             Select Case caseFlavour
  873.                                 Case "All Flavours"
  874.                                 'Depends on number of manufactueres with chosen Brand and chosen Sub Brand
  875.                                
  876.                                     For v = 4 To lastRow
  877.                                         For i = 0 To 28
  878.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"), Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"), Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"))
  879.                                         Next i
  880.                                     Next v
  881.                        
  882.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  883.                        
  884.                                     'Adds table rows to match number in j
  885.                                    Do While neededRowCount > currentRowCount
  886.                                         tbl.ListRows.Add AlwaysInsert:=True
  887.                                         currentRowCount = currentRowCount + 1
  888.                                     Loop
  889.                        
  890.                                     'Deletes table rows to match number in j
  891.                                    Do While currentRowCount > neededRowCount
  892.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  893.                                             tbl.ListRows(currentRowCount).Delete
  894.                                         Next currentRowCount
  895.                                     Loop
  896.                        
  897.                                     'When table rows matches j this will tell you to when you can add data
  898.                                    If currentRowCount = neededRowCount Then
  899.                                         MsgBox ("The Data will be added when you press OK")
  900.                                     End If
  901.                                    
  902.                                         For k = 0 To 28
  903.                                             For v = 1 To countryLastRow(k)
  904.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") And Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") And Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") Then
  905.                                                     For p = t To currentRowCount
  906.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  907.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  908.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  909.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  910.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  911.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  912.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  913.                                                        Exit For
  914.                                                     Next p
  915.                                                     t = t + 1
  916.                                                 End If
  917.                                             Next v
  918.                                             t = 1
  919.                                         Next k
  920.                                        
  921.                                         Application.ScreenUpdating = True
  922.                                         Application.Calculation = xlCalculationAutomatic
  923.                                         Application.EnableEvents = True
  924.                                
  925.                                 Case "Chosenfl"
  926.                                 'Depends on number of manufactueres with chosen Brand, chosen Sub Brand and chosen Flavour
  927.                                
  928.                                     For v = 4 To lastRow
  929.                                         For i = 0 To 28
  930.                                             countryRowsBegin(i) = countryRowsBegin(i) + Application.WorksheetFunction.CountIfs(Sheet3.Cells(v, countryRanges(i)), Sheet1.Range("C3"), Sheet3.Cells(v, countryRanges(i) + 1), Sheet1.Range("C4"), Sheet3.Cells(v, countryRanges(i) + 2), Sheet1.Range("C5"), Sheet3.Cells(v, countryRanges(i) + 3), Sheet1.Range("C6"))
  931.                                         Next i
  932.                                     Next v
  933.                        
  934.                                     neededRowCount = WorksheetFunction.Max(countryRowsBegin)
  935.                        
  936.                                     'Adds table rows to match number in j
  937.                                    Do While neededRowCount > currentRowCount
  938.                                         tbl.ListRows.Add AlwaysInsert:=True
  939.                                         currentRowCount = currentRowCount + 1
  940.                                     Loop
  941.                        
  942.                                     'Deletes table rows to match number in j
  943.                                    Do While currentRowCount > neededRowCount
  944.                                         For currentRowCount = currentRowCount To neededRowCount + 1 Step -1
  945.                                             tbl.ListRows(currentRowCount).Delete
  946.                                         Next currentRowCount
  947.                                     Loop
  948.                        
  949.                                     'When table rows matches j this will tell you to when you can add data
  950.                                    If currentRowCount = neededRowCount Then
  951.                                         MsgBox ("The Data will be added when you press OK")
  952.                                     End If
  953.                                    
  954.                                        
  955.                                         For k = 0 To 28
  956.                                             For v = 1 To countryLastRow(k)
  957.                                                 If Sheet3.Cells(v + 3, countryRanges(k)) = Sheet1.Range("C3") And Sheet3.Cells(v + 3, countryRanges(k) + 1) = Sheet1.Range("C4") And Sheet3.Cells(v + 3, countryRanges(k) + 2) = Sheet1.Range("C5") And Sheet3.Cells(v + 3, countryRanges(k) + 3) = Sheet1.Range("C6") Then
  958.                                                     For p = t To currentRowCount
  959.                                                         Sheet3.Cells(v + 3, countryRanges(k)).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) - 1).End(xlUp).Offset(1, 0)       'Manufacturer
  960.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 1).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k)).End(xlUp).Offset(1, 0)       'Brand
  961.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 2).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 1).End(xlUp).Offset(1, 0)   'SubBrand
  962.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 3).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 2).End(xlUp).Offset(1, 0)   'Flavour
  963.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 4).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 3).End(xlUp).Offset(1, 0)   'SKU
  964.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 5).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 4).End(xlUp).Offset(1, 0)   'Count
  965.                                                        Sheet3.Cells(v + 3, countryRanges(k) + 6).Copy Destination:=tbl.DataBodyRange(p, countryRanges(k) + 5).End(xlUp).Offset(1, 0)   'Size
  966.                                                        Exit For
  967.                                                     Next p
  968.                                                     t = t + 1
  969.                                                 End If
  970.                                             Next v
  971.                                             t = 1
  972.                                         Next k
  973.  
  974.                                        
  975.                                         Application.ScreenUpdating = True
  976.                                         Application.Calculation = xlCalculationAutomatic
  977.                                         Application.EnableEvents = True
  978.                                    
  979.                             End Select
  980.                     End Select
  981.             End Select
  982.     End Select
  983. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement