VBA to copy data based on column data to multiple files

LyonDer

New Member
Joined
Jul 1, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

I've looked up several posts on copying data from a report to multiple files based on the data in the first column. My original report uses rows 1 & 2 as the header so my data would start at A3. I want the header from rows 1 & 2 to copy to each new file with the filtered data. I've been unsuccessful in finding similar and my attempts to manipulate what I've found have been unsuccessful as well. Any help greatly appreciated.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
Thanks. Here is a box link for my file : Data Split Workbook Example.xlsx | Powered by Box

I want to reference the data in column A (A2 and down). I want to create new workbooks for each unique/filtered data entry in column A copying the data on each row to the new workbook. Each new workbook should also have the header (Row 1). This example file goes from column A to M, but with application there could be more or fewer columns.

I'd like to create the Module in my personal.xlsb and save the procedure to an icon on a customized toolbar.
 
Upvote 0
Change the workbook name (in red) to suit your needs. Please note that the worksheet name "Summary " in your file has a trailing space. Please delete that extra space.
Rich (BB code):
Sub CreateWorkbooks()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, v As Variant, dic As Object, i As Long
    Set srcWS = Workbooks("Data Split Workbook Example.xlsx").Sheets("Summary")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            With srcWS
                .Range("A1").CurrentRegion.AutoFilter 1, v(i, 1)
                Workbooks.Add
                .AutoFilter.Range.Copy Range("A1")
                .Columns.AutoFit
            End With
        End If
    Next i
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Change the workbook name (in red) to suit your needs. Please note that the worksheet name "Summary " in your file has a trailing space. Please delete that extra space.
Rich (BB code):
Sub CreateWorkbooks()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, v As Variant, dic As Object, i As Long
    Set srcWS = Workbooks("Data Split Workbook Example.xlsx").Sheets("Summary")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            With srcWS
                .Range("A1").CurrentRegion.AutoFilter 1, v(i, 1)
                Workbooks.Add
                .AutoFilter.Range.Copy Range("A1")
                .Columns.AutoFit
            End With
        End If
    Next i
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
This is great. Thank you! Since the Workbook could be under different names, is it possible to set the workbook to the current/active workbook instead of entering a static name? Also, I noticed data stops at column E even though there is additional data through column M. Is this because of blank filler columns. Can I have it copy all of the data as presented on the original workbook?
 
Upvote 0
Try:
VBA Code:
Sub CreateWorkbooks()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, v As Variant, dic As Object, i As Long
    Set srcWS = ActiveWorkbook.Sheets("Summary")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            With srcWS
                .Range("A1", Range("M" & Rows.Count).End(xlUp)).AutoFilter 1, v(i, 1)
                Workbooks.Add
                .AutoFilter.Range.Copy Range("A1")
                .Columns.AutoFit
            End With
        End If
    Next i
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
.Range("A1", Range("M" & Rows.Count).End(xlUp)).AutoFilter 1, v(i, 1)
I'm receiving a run-time error '1004': method range of object worksheet failed. Any reason why that would occur?
 
Upvote 0
Does it make any difference if you put a period "." before the 2nd use of Range eg
Rich (BB code):
.Range("A1", .Range("M" & Rows.Count).End(xlUp)).AutoFilter 1, v(i, 1)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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