M
Mike Magill
Hi,
I'm self taught so my coding is very inefficient. Up until now this
hasn't been a problem but my latest effort is taking far too long to
run. Can anyone advice on where the bottleneck is and how I can speed
it up?
Thanks
Mike
CODE BELOW (sorry for the amount)
--------------------
ActiveSheet.Unprotect Password:="XXXXXXX" ' Unprotects the
worksheet
Application.ScreenUpdating = False ' Stops the screen
refreshing while the macro is running
UserForm2.Show
' Reprotect the worksheet and end macro
Range("I4").Select
ActiveSheet.Protect Password:="XXXXXXX", DrawingObjects:=True,
Contents:=True, Scenarios:=True
Application.ScreenUpdating = True
UserForm2 CODE
----------------------
Private Sub CommandButton2_Click()
Dim x As Control
Dim PaperSize As String
Dim ReportType As String
For Each x In Frame2.Controls ' Loop through the option buttons
' within the Frame
If x.Value = True Then
PaperSize = Left(x.Caption, 2)
End If ' option button
Next
With ActiveSheet.PageSetup '
Set Page Setup
If PaperSize = "A3" Then
.PaperSize = xlPaperA3
Else
.PaperSize = xlPaperA4
End If
End With
If CheckBox1.Value = True Then
Columns("J:J").Select ' Hide Local Risk
Description column
Range("J6").Activate
Selection.EntireColumn.Hidden = True
Columns("BQ:BQ").Select ' Hide Local Risk
Description column
Range("BQ6").Activate
Selection.EntireColumn.Hidden = True
Else
End If
If CheckBox2.Value = True Then
Columns("C:F").Select ' Hide Assessment
Criteria columns
Selection.EntireColumn.Hidden = True
Columns("L:O").Select
Selection.EntireColumn.Hidden = True
Else
End If
For Each x In Frame1.Controls ' Loop through the option
buttons
' within the Frame
If x.Value = True Then
ReportType = x.Caption
End If ' option button
Next
Select Case ReportType
Case "Full Risk Register"
Call Print_Full_Register
Case "Summary Risk Register"
Call Print_Summary
Case "Red Individual Control Assessments"
Range("BL6").Value = "Red"
Call Overdue_Actions
Case "Amber Individual Control Assessments"
Range("BL6").Value = "Amber"
Call Overdue_Actions
Case "Red & Amber Individual Control Assessments"
Range("BL6").Value = "Red/Amber"
Call Overdue_Actions
Case "Overdue Actions"
Range("BL6").Value = "Action"
Call Overdue_Actions
Case Else
'Whatever
End Select
Columns("J:J").Select ' Unhide Local Risk
Description column
Range("J6").Activate
Selection.EntireColumn.Hidden = False
Columns("BQ:BQ").Select ' Unhide Local Risk
Description column
Range("BQ6").Activate
Selection.EntireColumn.Hidden = False
Columns("C:F").Select ' Show Assessment
Criteria columns
Selection.EntireColumn.Hidden = False
Columns("L:O").Select
Selection.EntireColumn.Hidden = False
Unload Me
End Sub
Sub Print_Full_Register()
'
' This macro prints the full risk register but only showing rows with
data
'
Call Row_Heights ' Macro to return heights of Rows 1-6 to their
preset heights
Call Action_Tidy_Up ' Macro to remove any extraneous spaces in
cells within the Action
' columns to highlight the need to complete
relevant actions properly
Call Print_Prep ' Macro to prepare rows for printing by ensure
all rows are expanded to show all text
Call Print_Report ' Macro to prepare rows for printing by ensure
only rows containing data are visible and then print
End Sub
Sub Print_Summary()
'
' This macro prints a summary report that excludes the control and
action details
'
Call Row_Heights ' Macro to return heights of Rows 1-6 to their
preset heights
Call Action_Tidy_Up ' Macro to remove any extraneous spaces in
cells within the Action
' columns to highlight the need to complete
relevant actions properly
Call Print_Prep ' Macro to prepare rows for printing by ensure
all rows are expanded to show all text
Rows("1:1").Select ' Hide control
panel when printing
Selection.EntireRow.Hidden = True
Columns("J:J").Select ' Hide Local Risk
Description
Selection.EntireColumn.Hidden = True
Selection.AutoFilter Field:=2, Criteria1:="<>" ' Filter data to
one line per risk
With ActiveSheet.PageSetup ' Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$A:$T"
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
If Application.Dialogs(xlDialogPrinterSetup).Show Then
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True '
Print one copy
Else
End If
With ActiveSheet.PageSetup ' Reset Page Setup
to default
.PrintArea = "$A:$AB"
End With
Rows("1:1").Select ' Unhide control
panel when printing
Selection.EntireRow.Hidden = False
Columns("J:J").Select ' Unhide Local
Risk Description
Selection.EntireColumn.Hidden = False
Selection.AutoFilter Field:=2 ' Filter data to
show all rows
End Sub
Sub Overdue_Actions()
'
' Overdue Actions Macro
' Macro recorded 01/06/2009 by mike.magill
'
'
Call Row_Heights ' Macro to return heights of Rows 1-6 to their
preset heights
Call Action_Tidy_Up ' Macro to remove any extraneous spaces in
cells within the Action
' columns to highlight the need to complete
relevant actions properly
' Check the user wants to print the risk register
Columns("BM:CB").Select
Selection.EntireColumn.Hidden = False
Selection.AutoFilter Field:=64 ' Filter to show
just Overdue Actions
Selection.AutoFilter Field:=64, Criteria1:=Range("BL6")
Call Print_Prep ' Macro to prepare rows for printing by ensure
all rows are expanded to show all text
With ActiveSheet.PageSetup '
Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$BM:$CB"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Rows("1:1").Select
Selection.EntireRow.Hidden = True '
Hide control panel when printing
If Application.Dialogs(xlDialogPrinterSetup).Show Then
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True '
Print one copy
Else
End If
Rows("1:1").Select '
Reveal control panel
Selection.EntireRow.Hidden = False
With ActiveSheet.PageSetup '
Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$A:$AB"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Selection.AutoFilter Field:=64 ' Filter to show
just Overdue Actions
Columns("BM:CB").Select
Selection.EntireColumn.Hidden = True
Range("i4").Select
End Sub
Sub Row_Heights()
'
' Macro to return heights of Rows 1-6 to their preset heights
'
Rows("1:1").Select
Selection.RowHeight = 132.75
Rows("2:2").Select
Selection.RowHeight = 20.25
Rows("3:3").Select
Selection.RowHeight = 15.75
Rows("4:4").Select
Selection.RowHeight = 19.5
Rows("5:5").Select
Selection.RowHeight = 19.5
Rows("6:6").Select
Selection.RowHeight = 38.25
End Sub
Sub Action_Tidy_Up()
'
' Macro to remove any extraneous spaces in cells within the Action
' columns to highlight the need to complete relevant actions properly
Dim rng As Range
Set rng = Range("y7:ab310")
Dim cell As Range
For Each cell In rng
If Trim(cell.Value) = "" Then
ActiveSheet.Range(cell, cell).ClearContents
End If
Next
End Sub
Sub Print_Prep()
'
' Macro to prepare rows for printing by ensure all rows are expanded
to
' show all text
Rows("7:310").Select
Range("b7").Activate
Rows("7:310").EntireRow.AutoFit
End Sub
Sub Print_Report()
'
' Macro to prepare rows for printing by ensure only rows containing
data
' are visible and then print one copy
With ActiveSheet.PageSetup '
Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$A:$AB"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Rows("1:1").Select
Selection.EntireRow.Hidden = True '
Hide control panel when printing
Selection.AutoFilter Field:=30, Criteria1:="x" '
Hide rows with no data
If Application.Dialogs(xlDialogPrinterSetup).Show Then
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True '
Print one copy
Else
End If
Selection.AutoFilter Field:=30 '
Show rows with no data
Rows("1:1").Select '
Reveal control panel
Selection.EntireRow.Hidden = False
End Sub
I'm self taught so my coding is very inefficient. Up until now this
hasn't been a problem but my latest effort is taking far too long to
run. Can anyone advice on where the bottleneck is and how I can speed
it up?
Thanks
Mike
CODE BELOW (sorry for the amount)
--------------------
ActiveSheet.Unprotect Password:="XXXXXXX" ' Unprotects the
worksheet
Application.ScreenUpdating = False ' Stops the screen
refreshing while the macro is running
UserForm2.Show
' Reprotect the worksheet and end macro
Range("I4").Select
ActiveSheet.Protect Password:="XXXXXXX", DrawingObjects:=True,
Contents:=True, Scenarios:=True
Application.ScreenUpdating = True
UserForm2 CODE
----------------------
Private Sub CommandButton2_Click()
Dim x As Control
Dim PaperSize As String
Dim ReportType As String
For Each x In Frame2.Controls ' Loop through the option buttons
' within the Frame
If x.Value = True Then
PaperSize = Left(x.Caption, 2)
End If ' option button
Next
With ActiveSheet.PageSetup '
Set Page Setup
If PaperSize = "A3" Then
.PaperSize = xlPaperA3
Else
.PaperSize = xlPaperA4
End If
End With
If CheckBox1.Value = True Then
Columns("J:J").Select ' Hide Local Risk
Description column
Range("J6").Activate
Selection.EntireColumn.Hidden = True
Columns("BQ:BQ").Select ' Hide Local Risk
Description column
Range("BQ6").Activate
Selection.EntireColumn.Hidden = True
Else
End If
If CheckBox2.Value = True Then
Columns("C:F").Select ' Hide Assessment
Criteria columns
Selection.EntireColumn.Hidden = True
Columns("L:O").Select
Selection.EntireColumn.Hidden = True
Else
End If
For Each x In Frame1.Controls ' Loop through the option
buttons
' within the Frame
If x.Value = True Then
ReportType = x.Caption
End If ' option button
Next
Select Case ReportType
Case "Full Risk Register"
Call Print_Full_Register
Case "Summary Risk Register"
Call Print_Summary
Case "Red Individual Control Assessments"
Range("BL6").Value = "Red"
Call Overdue_Actions
Case "Amber Individual Control Assessments"
Range("BL6").Value = "Amber"
Call Overdue_Actions
Case "Red & Amber Individual Control Assessments"
Range("BL6").Value = "Red/Amber"
Call Overdue_Actions
Case "Overdue Actions"
Range("BL6").Value = "Action"
Call Overdue_Actions
Case Else
'Whatever
End Select
Columns("J:J").Select ' Unhide Local Risk
Description column
Range("J6").Activate
Selection.EntireColumn.Hidden = False
Columns("BQ:BQ").Select ' Unhide Local Risk
Description column
Range("BQ6").Activate
Selection.EntireColumn.Hidden = False
Columns("C:F").Select ' Show Assessment
Criteria columns
Selection.EntireColumn.Hidden = False
Columns("L:O").Select
Selection.EntireColumn.Hidden = False
Unload Me
End Sub
Sub Print_Full_Register()
'
' This macro prints the full risk register but only showing rows with
data
'
Call Row_Heights ' Macro to return heights of Rows 1-6 to their
preset heights
Call Action_Tidy_Up ' Macro to remove any extraneous spaces in
cells within the Action
' columns to highlight the need to complete
relevant actions properly
Call Print_Prep ' Macro to prepare rows for printing by ensure
all rows are expanded to show all text
Call Print_Report ' Macro to prepare rows for printing by ensure
only rows containing data are visible and then print
End Sub
Sub Print_Summary()
'
' This macro prints a summary report that excludes the control and
action details
'
Call Row_Heights ' Macro to return heights of Rows 1-6 to their
preset heights
Call Action_Tidy_Up ' Macro to remove any extraneous spaces in
cells within the Action
' columns to highlight the need to complete
relevant actions properly
Call Print_Prep ' Macro to prepare rows for printing by ensure
all rows are expanded to show all text
Rows("1:1").Select ' Hide control
panel when printing
Selection.EntireRow.Hidden = True
Columns("J:J").Select ' Hide Local Risk
Description
Selection.EntireColumn.Hidden = True
Selection.AutoFilter Field:=2, Criteria1:="<>" ' Filter data to
one line per risk
With ActiveSheet.PageSetup ' Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$A:$T"
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
If Application.Dialogs(xlDialogPrinterSetup).Show Then
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True '
Print one copy
Else
End If
With ActiveSheet.PageSetup ' Reset Page Setup
to default
.PrintArea = "$A:$AB"
End With
Rows("1:1").Select ' Unhide control
panel when printing
Selection.EntireRow.Hidden = False
Columns("J:J").Select ' Unhide Local
Risk Description
Selection.EntireColumn.Hidden = False
Selection.AutoFilter Field:=2 ' Filter data to
show all rows
End Sub
Sub Overdue_Actions()
'
' Overdue Actions Macro
' Macro recorded 01/06/2009 by mike.magill
'
'
Call Row_Heights ' Macro to return heights of Rows 1-6 to their
preset heights
Call Action_Tidy_Up ' Macro to remove any extraneous spaces in
cells within the Action
' columns to highlight the need to complete
relevant actions properly
' Check the user wants to print the risk register
Columns("BM:CB").Select
Selection.EntireColumn.Hidden = False
Selection.AutoFilter Field:=64 ' Filter to show
just Overdue Actions
Selection.AutoFilter Field:=64, Criteria1:=Range("BL6")
Call Print_Prep ' Macro to prepare rows for printing by ensure
all rows are expanded to show all text
With ActiveSheet.PageSetup '
Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$BM:$CB"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Rows("1:1").Select
Selection.EntireRow.Hidden = True '
Hide control panel when printing
If Application.Dialogs(xlDialogPrinterSetup).Show Then
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True '
Print one copy
Else
End If
Rows("1:1").Select '
Reveal control panel
Selection.EntireRow.Hidden = False
With ActiveSheet.PageSetup '
Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$A:$AB"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Selection.AutoFilter Field:=64 ' Filter to show
just Overdue Actions
Columns("BM:CB").Select
Selection.EntireColumn.Hidden = True
Range("i4").Select
End Sub
Sub Row_Heights()
'
' Macro to return heights of Rows 1-6 to their preset heights
'
Rows("1:1").Select
Selection.RowHeight = 132.75
Rows("2:2").Select
Selection.RowHeight = 20.25
Rows("3:3").Select
Selection.RowHeight = 15.75
Rows("4:4").Select
Selection.RowHeight = 19.5
Rows("5:5").Select
Selection.RowHeight = 19.5
Rows("6:6").Select
Selection.RowHeight = 38.25
End Sub
Sub Action_Tidy_Up()
'
' Macro to remove any extraneous spaces in cells within the Action
' columns to highlight the need to complete relevant actions properly
Dim rng As Range
Set rng = Range("y7:ab310")
Dim cell As Range
For Each cell In rng
If Trim(cell.Value) = "" Then
ActiveSheet.Range(cell, cell).ClearContents
End If
Next
End Sub
Sub Print_Prep()
'
' Macro to prepare rows for printing by ensure all rows are expanded
to
' show all text
Rows("7:310").Select
Range("b7").Activate
Rows("7:310").EntireRow.AutoFit
End Sub
Sub Print_Report()
'
' Macro to prepare rows for printing by ensure only rows containing
data
' are visible and then print one copy
With ActiveSheet.PageSetup '
Set Page Setup
.PrintTitleRows = "$2:$6"
.PrintTitleColumns = ""
.PrintArea = "$A:$AB"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Rows("1:1").Select
Selection.EntireRow.Hidden = True '
Hide control panel when printing
Selection.AutoFilter Field:=30, Criteria1:="x" '
Hide rows with no data
If Application.Dialogs(xlDialogPrinterSetup).Show Then
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True '
Print one copy
Else
End If
Selection.AutoFilter Field:=30 '
Show rows with no data
Rows("1:1").Select '
Reveal control panel
Selection.EntireRow.Hidden = False
End Sub