Use VBA to copy infor to new sheets with formats

MPatter

New Member
Joined
Mar 14, 2011
Messages
3
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
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Warship

Well-known Member
Joined
Jul 17, 2007
Messages
1,051
Maybe setup an empty worksheet with the formatting you need.
Then in your VBA copy this "template" sheet instead of creating a new sheet.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,624
Messages
5,523,965
Members
409,547
Latest member
AW2020

This Week's Hot Topics

Top