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]