VBA to copy rows from worksheet to multiple other worksheets based on cell data

LFaz268710

New Member
Joined
Mar 3, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a workbook that contains the details of customers booked for different trips on sheet 2

I want to copy the rows from sheet 2 to different sheets based on the location, e.g. all customers booked for Nice will copy to sheet 3, all for Mallorca will go to sheet 4 etc.

Is a VBA the only way to do this? I need to make sure it doesn't delete the data on sheet 2, but I'm struggling with the standard copy code as I can't work out how to ensure each additional sheet only pulls the particular location over. Do I have to have a VBA module for each location?

Many thanks,
 
Yes that's correct - the owner of the business has to data cleanse these items so they will eventually have the correct format.
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The macro is designed to look for the event name which includes the full date. Will you run the macro only after all the events contain the full date? If not, I need to know how you want to handle the events without a full date or with no date all as part of their name.
 
Upvote 0
The macro will run monthly, so new bookings will be added on a regular basis and then each month the macro will be run to update the location sheets.
It can skip over any that do not have the right criteria, as all rows need to remain on the CRM sheet but copy across to the location sheet if there is a match.
 
Upvote 0
Try this macro.
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, v As Variant, srcWS As Worksheet, dic As Object, key As Variant
    Set srcWS = Sheets("CRM")
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = srcWS.Range("D2", srcWS.Range("D" & srcWS.Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(v)
        If Not dic.Exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
        End If
    Next i
    For Each key In dic.keys
        With srcWS
            If Evaluate("isref('" & key & "'!A1)") Then
                .Range("A1").AutoFilter 4, key
               .AutoFilter.Range.Offset(1).Copy Sheets(key).Cells(Sheets(key).Rows.Count, "A").End(xlUp).Offset(1)
            End If
        End With
    Next key
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,863
Messages
6,121,978
Members
449,058
Latest member
oculus

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