C
Chutney
I have a workbook the has 60 worksheets (created by Access). I need a
macro to format the title row, auto-size the columns, and set the
print settings on each worksheet. All of the settings are identical
for all of the worksheets.
I have tried to create a macro to do this. The macro will format all
of the title rows on all of the worksheets but it sets the column
widths and print settings only on the first (active) worksheet. I have
searched the forums about this but the only solution I have found is
to iterate through all of the worksheets and set them one-by-one. That
takes forever. I can manually select all of the worksheets and do all
of the settings in one go far faster faster than using a macro to
iterate through each of the worksheets but I need to automate this
process. Any suggests about how to get Excel to do programatically and
efficiently what I can do manually?
This is my current macro. I want to eliminate the iteration or, if
that is not possible, make it run as efficiently as possible:
Sub Format_Worksheets()
Application.Visible = False
Application.ScreenUpdating = False
'Create variables
Dim iGroupNo As Integer
Dim oWS As Worksheet
Dim sGroup As String
'Set format of column title cells for all worksheets
Worksheets.Select 'select all worksheets in workbook
Worksheets(1).Activate 'can activate only one sheet at a time
but formats apply to all selected sheets
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
Selection.Font.Bold = True
'Set print format and column widths for each individual worksheet
iGroupNo = 0
For Each oWS In Worksheets
iGroupNo = iGroupNo + 1
sGroup = "Group " & iGroupNo
oWS.Activate
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.LeftHeader = "Printed: &D"
.CenterHeader = "Report for: &A"
.RightHeader = "Page &P of &N"
.CenterFooter = ""
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 100
End With
ActiveSheet.Cells.Select
Selection.EntireColumn.AutoFit
Range("A2").Select
ActiveWindow.FreezePanes = True
Next oWS
Worksheets(1).Activate
Range("A2").Select
Application.Visible = True
Application.ScreenUpdating = True
End Sub
macro to format the title row, auto-size the columns, and set the
print settings on each worksheet. All of the settings are identical
for all of the worksheets.
I have tried to create a macro to do this. The macro will format all
of the title rows on all of the worksheets but it sets the column
widths and print settings only on the first (active) worksheet. I have
searched the forums about this but the only solution I have found is
to iterate through all of the worksheets and set them one-by-one. That
takes forever. I can manually select all of the worksheets and do all
of the settings in one go far faster faster than using a macro to
iterate through each of the worksheets but I need to automate this
process. Any suggests about how to get Excel to do programatically and
efficiently what I can do manually?
This is my current macro. I want to eliminate the iteration or, if
that is not possible, make it run as efficiently as possible:
Sub Format_Worksheets()
Application.Visible = False
Application.ScreenUpdating = False
'Create variables
Dim iGroupNo As Integer
Dim oWS As Worksheet
Dim sGroup As String
'Set format of column title cells for all worksheets
Worksheets.Select 'select all worksheets in workbook
Worksheets(1).Activate 'can activate only one sheet at a time
but formats apply to all selected sheets
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
Selection.Font.Bold = True
'Set print format and column widths for each individual worksheet
iGroupNo = 0
For Each oWS In Worksheets
iGroupNo = iGroupNo + 1
sGroup = "Group " & iGroupNo
oWS.Activate
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.LeftHeader = "Printed: &D"
.CenterHeader = "Report for: &A"
.RightHeader = "Page &P of &N"
.CenterFooter = ""
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 100
End With
ActiveSheet.Cells.Select
Selection.EntireColumn.AutoFit
Range("A2").Select
ActiveWindow.FreezePanes = True
Next oWS
Worksheets(1).Activate
Range("A2").Select
Application.Visible = True
Application.ScreenUpdating = True
End Sub