RunTime Error 1004 for Copy to New Workbook

postelrich

New Member
Joined
Feb 24, 2011
Messages
31
Hi, I have one master file where I need to split out and send data 15 different regions. I've tried make a macro to automate it by using autofilter and copy to achieve it but I keep getting a Run time Error 1004. I am using Excel 2007, my code below though I cut out the values for the string array. If I step into the code, the error pops up when I get to the copy line.

Code:
Sub SplitFile()
Dim regions(15) As String
regions(0) = "FLORIDA"

For N = LBound(regions) To UBound(regions)
    Set wb = Workbooks.Add
       
    With ThisWorkbook.Sheets("MASTER")
        .Activate
        .AutoFilterMode = False
        .Range("C:BY").AutoFilter
        .Range("C:BY").AutoFilter Field:=3, Criteria:=regions(N)
        .Range("C:BY").SpecialCells(xlCellTypeVisible).Select
        .Range("C:BY").Copy
    End With
    
    With wb
        .Sheet1.Paste
        .SaveAs Filename:="C:\Regional Files\" & regions(N)
        .Close True
    End With
    
    Set wb = Nothing
Next N
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Why not split the data out into separate worksheets in the current workbook and then copy those individual worksheets to new workbooks?

As for the error, perhaps that could be because you are looping through an array where only 1 item has a value, eg FLORIDA, and the others are empty.
 
Upvote 0
Well the point is to avoid having to do it manually. I could copy the information to the new workbooks just as easily as I could copy it to a new worksheet. Unless you meant to do it through vba of course.

I should state that the error occurs when it tries to filter by criteria.

The rest of the array does have value, just left it out for posting here.
 
Upvote 0
Who said anything about doing anything manually?

The whole thing can be done in code.
 
Upvote 0
hmmm my basic understanding of vba would probably lead to similar errors if i try to go the other way. I found the 1004 error to be caused by not including a 1 in criteria:= . Now i get run time error 438 now when it tries to paste:confused:
 
Upvote 0
To be honest I can't help you with that error or the original code.

However I might be able to post code that will split out the data to separate worksheets and create new workbooks from them

I'd need some info about the data and how you are splitting it out.

For example:

What range is the data in, does it have headers?

Do you want to split it out by based on a particular column/header?

Is the data to be split based on the unique values in that column?
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

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