Split Worksheet Into Multiple Workbooks Based on Column Value

phipsijuice

New Member
Joined
Apr 17, 2015
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi!

Using VBA I'd like to split an Excel worksheet into multiple workbooks based on each unique value in Column B. The header name for Column B is "Acronym". Each Workbook would contain the same column headers as the master file. The Excel format is .xlsx

I want the file name to be "Column B Value Quarterly Shipping Report"; an example would be "NAHB Quarterly Shipping Report" The workbooks would be split in the same folder as the master file.

Any help would be greatly appreciated!

Stephen
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This should do the trick for you:
Code:
Sub CopyWorksheet()Dim b As Integer, lastrow As Integer, lastcol As Integer
Dim wb As Workbook, wbCurr As Workbook
Dim strPath As String, strVal As String


strPath = Application.ActiveWorkbook.Path
Set wbCurr = ActiveWorkbook
lastrow = ActiveSheet.Range("b100000").End(xlUp).Row
lastcol = ActiveSheet.Range("xfd1").End(xlToLeft).Column
For b = 2 To lastrow
    strVal = Range("b" & b)
    Set wb = Workbooks.Add
    ThisWorkbook.Activate
    ActiveSheet.Range(Cells(1, 1), Cells(1, lastcol)).Copy
    wb.Activate
    wb.ActiveSheet.Range("a1").PasteSpecial (xlPasteValues)
    wb.SaveAs strPath & "\" & strVal & "Quarterly Shipping Report.xlsx"
    wbCurr.Activate
Next b
End Sub

It copies the top row (header) to each new workbook with name based on the Column B value and path based on the original workbook's path.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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