I think most people would just use a subroutine call or a function.
(And finding old code and making it work seems like a very reasonable approach
to me--thank goodness for google!)
For example:
Option Explicit
Sub testme()
Dim curWks As Worksheet
Dim newWks As Worksheet
Dim myCell As Range
Dim myRng As Range
Dim Place_holder As Long ' Added by JPalmer
Place_holder = 1 ' Added by JPalmer
Set curWks = Worksheets("sheet1")
With curWks
' Added by JPalmer, only print if Variance over $200
If .Range("VarianceTest").Value > 200 Then
'keep going
Else
MsgBox "Variance Test is not large enough!"
Exit Sub
End If
Set newWks = Worksheets.Add
.Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=newWks.Range("A1"), Unique:=True
End With
With newWks
Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With
With curWks
.AutoFilterMode = False
For Each myCell In myRng.Cells
.Range("a:a").AutoFilter Field:=1, Criteria1:=myCell.Value
Call Page_Number(curWks, Place_holder)
.PrintOut preview:=True 'used for testing only
'.PrintOut Copies:=1, Collate:=True
Place_holder = Place_holder + 1 ' added by JPalmer
Next myCell
.AutoFilterMode = False
End With
Application.DisplayAlerts = False
newWks.Delete
Application.DisplayAlerts = True
End Sub
Sub Page_Number(curWks As Worksheet, Place_holder As Long)
With curWks.PageSetup
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.PrintArea = ""
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = "&16&HPage number " & Place_holder
' added by Jpalmer to increment page Number
.LeftFooter = "&8&H&D &T"
.CenterFooter = ""
.RightFooter = "&8&H&Z" & Chr(10) & "&F"
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.4921259845)
.FooterMargin = Application.InchesToPoints(0.4921259845)
.PrintHeadings = False
.PrintGridlines = True
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintErrors = xlPrintErrorsDisplayed
End With
End Sub
The other thing you may want to do is to just set the page setup properties that
are changing. Each one of those changes (whether or not it's a real change,
will slow your procedure down).
Maybe the only thing you have to keep would be:
Sub Page_Number(curWks As Worksheet, Place_holder As Long)
With curWks.PageSetup
' added by Jpalmer to increment page Number
.RightHeader = "&16&HPage number " & Place_holder
End With
End Sub
==
I did move the check for the value in variancetest (on the same worksheet????)
higher in the code. It wouldn't have to be checked each time and there's no
reason to insert a new sheet if you won't be using it.