Hello,
I will have a budget spreadsheet with atleast 100 departments. It will be in Excel 2007 with protected cells and freezed panes. I need to figure out how to breack out each department into its own sheet (With All formating in Tact, freeze panes and all). Next I will have to create a workbook for each sheet and email each sheet to a different department head. The following macro breakes out into sheets but does not keep the formating. Can someone tell me what I must insert to have the format copies over as well?
Private Sub DeptTabs()
Dim strSrcSheet As String
Dim rngSrcStart As Range
Dim rngSrcEnd As Range
Dim rngCell As Range
Dim strLastDept As String
Dim intDestRow As Integer
On Error GoTo ErrHnd
'name of source data worksheet (tab)
strSrcSheet = "SrcData"
With ActiveWorkbook
'setup source range in column B
Set rngSrcStart = .Worksheets(strSrcSheet).Range("B2")
Set rngSrcEnd = .Worksheets(strSrcSheet).Range("B65534").End(xlUp)
'set destination row counter
intDestRow = 1
'set last department name
strLastDept = ""
'loop through cells in column B
For Each rngCell In Range(rngSrcStart, rngSrcEnd)
'test if department ID change
If rngCell.Text <> strLastDept Then
'create new sheet
.Worksheets.Add After:=.Worksheets(Worksheets.Count)
'name new sheet
.Worksheets(Worksheets.Count).Name = rngCell.Text
'copy header row
.Worksheets(strSrcSheet).Range("A1").EntireRow.Copy _
Destination:=.Worksheets(rngCell.Text).Range("A1")
'reset variables
strLastDept = rngCell.Text
intDestRow = 1
End If
'copy entire row
rngCell.EntireRow.Copy _
Destination:=.Worksheets(strLastDept).Range("A1").Offset(intDestRow, 0)
'increment row counter
intDestRow = intDestRow + 1
Next rngCell
End With
Exit Sub
'error handler
ErrHnd:
Err.Clear
End Sub
I will have a budget spreadsheet with atleast 100 departments. It will be in Excel 2007 with protected cells and freezed panes. I need to figure out how to breack out each department into its own sheet (With All formating in Tact, freeze panes and all). Next I will have to create a workbook for each sheet and email each sheet to a different department head. The following macro breakes out into sheets but does not keep the formating. Can someone tell me what I must insert to have the format copies over as well?
Private Sub DeptTabs()
Dim strSrcSheet As String
Dim rngSrcStart As Range
Dim rngSrcEnd As Range
Dim rngCell As Range
Dim strLastDept As String
Dim intDestRow As Integer
On Error GoTo ErrHnd
'name of source data worksheet (tab)
strSrcSheet = "SrcData"
With ActiveWorkbook
'setup source range in column B
Set rngSrcStart = .Worksheets(strSrcSheet).Range("B2")
Set rngSrcEnd = .Worksheets(strSrcSheet).Range("B65534").End(xlUp)
'set destination row counter
intDestRow = 1
'set last department name
strLastDept = ""
'loop through cells in column B
For Each rngCell In Range(rngSrcStart, rngSrcEnd)
'test if department ID change
If rngCell.Text <> strLastDept Then
'create new sheet
.Worksheets.Add After:=.Worksheets(Worksheets.Count)
'name new sheet
.Worksheets(Worksheets.Count).Name = rngCell.Text
'copy header row
.Worksheets(strSrcSheet).Range("A1").EntireRow.Copy _
Destination:=.Worksheets(rngCell.Text).Range("A1")
'reset variables
strLastDept = rngCell.Text
intDestRow = 1
End If
'copy entire row
rngCell.EntireRow.Copy _
Destination:=.Worksheets(strLastDept).Range("A1").Offset(intDestRow, 0)
'increment row counter
intDestRow = intDestRow + 1
Next rngCell
End With
Exit Sub
'error handler
ErrHnd:
Err.Clear
End Sub