Master Workbook Many Workbooks Multiple Sheets VBA Macro

xcelnovice

Board Regular
Joined
Dec 31, 2011
Messages
81
Hello, below is a macro that looks at a single sheet and parses it out to many sheets. This is a watered down version of what I need just so I could follow along & get a better understanding of writing macros so i just used the first sheet of my workbook I intend to use the macro on. The workbook has 4 more sheets. What is the best way of going about applying this to the other sheets in my workbook? Should I copy and paste this code 4 more times below & modify it to work with my other sheets? Is there a better approach? Also any other tips or advice is welcomed. I wasn't able to get the "save" part to work but I haven't spent as much time on that yet. Thanks

Code:
[COLOR=#333333][COLOR=#444444][FONT=Calibri]Sub CreateWBS()[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]    Dim WBO As Workbook 'Master[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]    Dim WBN As Workbook 'New[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]    Dim WSO As Worksheet 'Original[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]    Dim WSN As Worksheet 'New Worksheet[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]     Set WBO = ActiveWorkbook[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]    Set WSO = ActiveSheet[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]     FinalRow = WSO.Cells(Rows.Count, 1).End(xlUp).Row + 1   'Find final Row[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]     LastDept = Cells(2, 1)[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]    StartRow = 2[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]    For i = 2 To FinalRow[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]        ThisDept = WSO.Cells(i, 1)[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]        If ThisDept = LastDept Then 'Do nothing[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]         Else[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            'We have a new dept starting[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            'Copy all of the previous rows to a new wkbk[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            LastRow = i - 1[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            RowCount = LastRow - StartRow + 1 'How many rows you want to copy[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]             'Create a new workbook[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            Set WBN = Workbooks.Add(Template:=xlWBATWorksheet)[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            Set WSN = WBN.Worksheets(1)[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]             'Set up headings[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            WSN.Cells(1, 1).Value = "Budget Summary"[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            WSN.Cells(2, 1).Value = LastDept & " - " & WSO.Cells(StartRow, 2)[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            WSO.Range("A1:K1").Copy Destination:=WSN.Cells(4, 1)[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]             'Copy all records[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            WSO.Range(WSO.Cells(StartRow, 1), WSO.Cells(LastRow, 11)).Copy Destination:=WSN.Cells(5, 1)[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]              'FN = LastDept & ".xlsx"[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            'FP = WBO & Application.PathSeparator[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            'WBN.SaveAs Filename:=FP & FN[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            'WBN.Close SaveChanges:=False[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]             LastDept = ThisDept[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]            StartRow = i[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]        End If[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]        Next i[/FONT][/COLOR]
[COLOR=#444444][FONT=Calibri]End Sub[/FONT][/COLOR]
[/COLOR]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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