Earlier we have split our data into separate excel sheet but this time, we will split spreadsheet records into a new workbook. It’s useful and reduces our effort in making different workbooks with the same data.
I hope you have enjoyed our earlier blogs, revisit “How to split excel data into separate spreadsheets using VBA“.
We take our earlier data which looks like below:
Excel VBA Macros for Non-Coders
Do you find VBA Macros scary? Did you miss any job opportunity because of it? Get started today with our eBook guide on using – Excel VBA Macros. 140 pages of rich visuals. Download now.
Please look at the column state. If you do it manually then you need to apply a filter and then chose one state name. After that, you need to copy the records and press Ctrl + N to create a new workbook and then paste the records. And then you need to save the file in your desired location. After that, you will go to the second state.
Just think how long time will it take?
Not only that if any new records inserted into your raw sheet you need to do the entire stuff once again. But at the same time, a single piece of code will automate the entire process.
If it’s your first time with Macro then Click here to try our Excel macro tutorial.
Sub ExtractToNewWorkbook() Dim ws As Worksheet Dim wsNew As Workbook Dim rData As Range Dim rfl As Range Dim state As String Dim sfilename As String Set ws = ThisWorkbook.Sheets("emp") 'Apply advance filter in your sheet With ws Set rData = .Range(.Cells(1, 1), .Cells(.Rows.Count, 11).End(xlUp)) .Columns(.Columns.Count).Clear .Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp)) .AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Cells(1, .Columns.Count), Unique:=True For Each rfl In .Range(.Cells(1, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp)) state = rfl.Text Set wsNew = Workbooks.Add sfilename = state & ".xlsx" 'Set the Location ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & sfilename Application.DisplayAlerts = False ws.Activate rData.AutoFilter Field:=6, Criteria1:=state rData.Copy Windows(state).Activate ActiveSheet.Paste ActiveWorkbook.Close SaveChanges:=True Next rfl Application.DisplayAlerts = True End With ws.Columns(Columns.Count).ClearContents rData.AutoFilter End Sub
In my case, I have saved all workbooks where this file is located. My file is saved in (“C:\Users\Desktop\State Name Separate in a New workbook”) this location.
So all the news files will be created and saved in the same location. Now my folder looks like below
Only one file is present there. Now if you run the code then all the new files will be created here and the folder looks like below picture:
It will take hardly 1 min to complete the task. Isn’t that interesting? Moreover, if you add new records in your raw spreadsheet what will have to do is you need to delete all the files before running the macro code.
You can also add a button in your excel sheet and assign the macro to that button. You have already discussed the same in our earlier blog.
First of all, I have applied an advanced filter on States in my raw spreadsheet. Then I have stored the entire state’s name in a variable.
Then I have applied a loop. Every time it will get a new state name it will create a new workbook and save the file in the location and then paste the records in the new workbook. After pasting the data in the new worksheet, it will close the new workbook.
The entire process will go on until it will copy and paste till the last state (Split spreadsheet data). If you wish you can add a msgbox at the end of your macro code to get the information that it has successfully run the macro code.
Note: Please note that before you run the code please check that there is no other file with the same name. For example, if you run the code for the second time it will give you the following error as the same file already exists in your location. It will look for your confirmation.
Now if you press the cancel button it will give you an error as shown below:
We can overcome the scenario in two ways:
When you are saving the new workbook, you need to check whether the file name exists or not. If it exists then you need to delete it from the folder or the best option is that when you are saving the file you can save the file with the current date.
To do the same, you need to add or change the below red color lines.
I will suggest to delete the earlier file and re-run the code.
Excel VBA Macros for Non-Coders
Do you find VBA Macros scary? Did you miss any job opportunity because of it? Get started today with our eBook guide on using – Excel VBA Macros. 140 pages of rich visuals. Download now.
can you take this a step further and create a pivot table for the cut data in each work book?
Windows(storeName).Activate on this line getting Error 9 in run time error: Subscript out of range.
Hi,
When we set the range, what does rFL stand for? And how can I change this to reference a table instead of .rows.count, 13 etc?
Can you please mail the file. We will get back to you
Hi,
I want to apply the advanced filter for multiple columns so how can i add that feature in this program, Please help me.
HI
I got Run-Time Error ‘9’
Subscript out of range
what i have to do know…?
HI
I got Run-Time Error ‘9’
Subscript out of range
what i have to do know…?
You can change the sheet name to “EMP” (without quotes)
Hi,
Great macro, thanks! I would like to ask if it is possible to keep the other worksheets of the document also.
I have a similar task ahead of me, and this macro is perfect for me, but I have some other worksheets in my workbook which would need to be saved as well (without any change on them)
HI
I got Run-Time Error ‘9’
Subscript out of range
Windows(state).Activate
This line is throwing the error, the file isnt saving with the State name, this is the part that isnt working
sfilename = state & “.xlsx”
HI there, it creates the files but doesn’t copy the data
Can you please reply, what is not working for you?
Dear Team
Pls help i am getting only heading column data, kindly help
Can you please reply with more details or send us an email