Copying row of data from table into other sheet if cell matches criteria VBA

AEla

New Member
Joined
Apr 25, 2022
Messages
3
Office Version
  1. 365
Platform
  1. MacOS
Screenshot 2022-04-25 at 2.28.54 PM.png


Good afternoon,
I have this table of data, and i would like to be able to copy each row of data into another sheet depending on the country name.
So if the country is Belgium for example, it would automatically go through the whole table (if data is added in the future) and take all the rows where the country is Belgium and paste them into another sheet named "Belgium". I would like to do this for every country.
What kind of VBA code would allow me to do so?


Please let me know if you can help.
Best regards,
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Do the individual country sheets already exist or does the macro have to create them?
 
Upvote 0
This macro assumes that the country sheets have headers in row 1 starting in cell A1. Change the name of the sheet (in red) to suit your needs.
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim i As Long, v As Variant, srcWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    v = srcWS.Range("B2", srcWS.Range("B" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Sheets(v(i, 1)).UsedRange.Offset(1).ClearContents
                With srcWS
                    .Range("B1").CurrentRegion.AutoFilter 1, v(i, 1)
                    .AutoFilter.Range.Offset(1).Copy Sheets(CStr(v(i, 1))).Range("A2")
                End With
            End If
        Next i
    End With
    srcWS.Range("B1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
This macro assumes that the country sheets have headers in row 1 starting in cell A1. Change the name of the sheet (in red) to suit your needs.
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim i As Long, v As Variant, srcWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    v = srcWS.Range("B2", srcWS.Range("B" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Sheets(v(i, 1)).UsedRange.Offset(1).ClearContents
                With srcWS
                    .Range("B1").CurrentRegion.AutoFilter 1, v(i, 1)
                    .AutoFilter.Range.Offset(1).Copy Sheets(CStr(v(i, 1))).Range("A2")
                End With
            End If
        Next i
    End With
    srcWS.Range("B1").AutoFilter
    Application.ScreenUpdating = True
End Sub
Thank you so much for you help and reply, however i am now getting an error message on the 'With create object("scripting dictionary")
Would you be able to help??
Best regards,
 

Attachments

  • Screenshot 2022-04-26 at 10.43.09 AM.png
    Screenshot 2022-04-26 at 10.43.09 AM.png
    162.5 KB · Views: 18
Upvote 0
I just noticed that you are using a Mac and VBA for the Mac does not recognize Scripting Dictionaries. Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, LastRow As Long, srcRng As Range, country As Range
    Set srcWS = Sheets("Sheet1")
    With srcWS
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("B1:G" & LastRow).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
        Set srcRng = .Range("C2:C" & LastRow).SpecialCells(xlCellTypeVisible)
        .ShowAllData
        For Each country In srcRng
            .Range("B1").CurrentRegion.AutoFilter 2, country
            .AutoFilter.Range.Offset(1).Copy Sheets(CStr(country)).Range("A2")
        Next country
        .Range("B1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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