I will try and explain wha I have done. The main part is to take the original code:
If Sheet11.Cells(3, 8) >= 2 Then
Sheets("Page (2)").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
If Sheet5.Cells(3, 8) >= 3 Then
Sheets("Page (3)").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
If Sheet6.Cells(3, 8) >= 4 Then
Sheets("Page (4)").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
If Sheet7.Cells(3, 8) >= 5 Then
Sheets("Page (5)").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
and replace it with a loop:
VArray = Array(Array("Sheet11", 2, "Page (2)"), Array("Sheet5", 3, "Page (3)"), Array("Sheet6", 4, "Page (4)"))
For Each var In VArray()
If Worksheets(var(0)).Cells(3, 8) >= var(1) Then
Sheets(var(2)).Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
Next
So how does it work? Well: in the original repeating sections there are three items that change. I have marked them in red below:
If Sheet11.Cells(3, 8) >= 2 Then
Sheets("Page (2)").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
If Sheet5.Cells(3, 8) >= 3 Then
Sheets("Page (3)").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
These I have picked out and placed in the nested array:
First block:
Array( "Sheet11" , 2 , "Page (2)" )
Second block:
Array( "Sheet5" , 3 , "Page (3)" )
and so on.
These blocks are then bundled into another array:
array( block1 , block2 , etc )
Array(Array("Sheet11", 2, "Page (2)"), Array("Sheet5", 3, "Page (3)"), Array("Sheet6", 4, "Page (4)"))
Then the remaining code uses this data:
For Each var In VArray()
If Worksheets(var(0)).Cells(3, 8) >= var(1) Then
Sheets(var(2)).Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
Next
basically steps through each block of data setting the variable var to contain the data.
var now contains an array of three items. The three items are identified as: var(0), var(1), and var(2). Which for the first block will contain :
var(0) = "Sheet11"
var(1) = 2
var(2) = "Page (2)"
So you can see, I hope, that as the variables contain the values from the block the code will look like the original:
If Worksheets(var(0)).Cells(3, 8) >= var(1) Then
Sheets(var(2)).Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If
I hope this helps
Bookmarks