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
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Maybe setup an empty worksheet with the formatting you need.
Then in your VBA copy this "template" sheet instead of creating a new sheet.
 
Upvote 0

Forum statistics

Threads
1,214,548
Messages
6,120,146
Members
448,948
Latest member
spamiki

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