VBA vlookup to Reference Sheet & Copy row data from Master Sheet to Specific sheets

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

Can you possibly help me out on the macro I'm trying to sort out? Basically, I have this excel file named "MasterList" with two tabs sheets namely: Master & Reference.
The master contains a list of data while reference sheet has list of countries and its equivalent country group.

Example in "Reference" Sheet:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Country[/TD]
[TD]Group[/TD]
[/TR]
[TR]
[TD]United States[/TD]
[TD]North America[/TD]
[/TR]
[TR]
[TD]Canada[/TD]
[TD]North America[/TD]
[/TR]
[TR]
[TD]Germany[/TD]
[TD]Germany[/TD]
[/TR]
[TR]
[TD]Norway[/TD]
[TD]Rest of Europe[/TD]
[/TR]
[TR]
[TD]Libya[/TD]
[TD]Rest of World[/TD]
[/TR]
</tbody>[/TABLE]


In the "Master" sheet, there's a country column which starts in cell W4 until the last blank cell (W***).

What the macro does:
*In Master Sheet - Look up the "Country" against the "Reference" and get its country "Group"
*For the Above example, 4 sheets will be created (North America, Germany, Rest ow World & Rest of Europe). All data rows that falls under each category will be copied to its designated tabs.


Any help will be much appreciated (I'm still learning about VBA) :) Thanks in advance!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
How about
Code:
Sub CopySplitData()
   Dim Cl As Range
   Dim ary As Variant
   Dim Mws As Worksheet
   Dim Rws As Worksheet
   Dim Ky As Variant
   
   Set Mws = Sheets("Master")
   Set Rws = Sheets("Reference")
   With CreateObject("scripting.dictionary")
      For Each Cl In Rws.Range("A2", Rws.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Cl.Offset(, 1).Value
         Else
            .Item(Cl.Value) = .Item(Cl.Value) & "|" & Cl.Offset(, 1).Value
         End If
      Next Cl
      If Mws.AutoFilterMode Then Mws.AutoFilterMode = False
      For Each Ky In .keys
         Mws.Range("A4:W4").AutoFilter 23, Split(.Item(Ky), "|"), xlFilterValues
         Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
         Mws.AutoFilter.Range.Copy ActiveSheet.Range("A1")
      Next Ky
      Mws.AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Thanks Fluff for the quick response. However, the sheets created by the macro are not based in "Group Country" (North America, Germany, Rest of World and etc). I was thinking in the "Master" Sheet, we can add 1 more column where it will look up the value of Country against the "Reference" and get its equivalent " Country Group" first. One thing, can you help me on the code wherein it will copy the format of "Master" sheet to every sheet that will be created while copying the header as well (column header in "Master sheet" is located in cell "A2". Many thanks in advance!
 
Upvote 0
Oops, got it the wrong way round try
Code:
Sub CopySplitData()
   Dim Cl As Range
   Dim Ary As Variant
   Dim Mws As Worksheet
   Dim Rws As Worksheet
   Dim Ky As Variant
   
   Set Mws = Sheets("Master")
   Set Rws = Sheets("Sheet1")
   With CreateObject("scripting.dictionary")
      For Each Cl In Rws.Range("B2", Rws.Range("B" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Cl.Offset(, -1).Value
         Else
            .Item(Cl.Value) = .Item(Cl.Value) & "|" & Cl.Offset(, -1).Value
         End If
      Next Cl
      If Mws.AutoFilterMode Then Mws.AutoFilterMode = False
      For Each Ky In .keys
         Mws.Range("A2:W2").AutoFilter 23, Split(.Item(Ky), "|"), xlFilterValues
         Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
         Mws.AutoFilter.Range.Copy ActiveSheet.Range("A1")
      Next Ky
      Mws.AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Your code works well, thanks Fluff! Just one more thing..how about the header for each sheet? I want to paste it in the 2nd row (A2-W2) and the country breakdown will start in the 4th row (A4-W4).
 
Upvote 0
Try
Code:
For Each Ky In .keys
         Mws.Range("A[COLOR=#ff0000]4[/COLOR]:W[COLOR=#ff0000]4[/COLOR]").AutoFilter 23, Split(.Item(Ky), "|"), xlFilterValues
         Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
         Mws.AutoFilter.Range.Copy ActiveSheet.Range("A[COLOR=#ff0000]2[/COLOR]")
      Next Ky
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Hi Fluff! Me again. I am having a problem on the format of the created tabs. Can we possibly just:
1. Create a copy of the Master Sheet
2. Rename it based on country region
3. Those country which don't belong on that region will be deleted on each sheet (based on reference sheet)

Thanks so much again in advance. :)
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,472
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top