I am trying to combine multiple macros into one. How do I add a loop so that it repeats itself 'x' amount of times, with 'x' being the values of Sheet3!A1, and then it executes the print preview function?
This is the VBA code I have so far. I cant seem to get the loop to work correctly. Im trying to work out what I need to add to "I CANT WORK OUT WHAT TO PUT HERE"
Sub Calculate()
' Macro to copy data to a new row, run it a specified number of times, and print output.
' Turn off screen updating.
Application.ScreenUpdating = False
' Declarations.
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Dim targetRange As Range
Dim loopCount As Long
Dim i As Long
Dim printSheet As Worksheet
' Set variables.
Set copySheet = Worksheets("Sheet5")
Set pasteSheet = Worksheets("Sheet6")
Set printSheet = Worksheets("Sheet6")
' Unlock pasteSheet to allow editing.
pasteSheet.Unprotect Password:="password"
' Set targetRange as the last cell in column C with a value.
Set targetRange = pasteSheet.Cells(pasteSheet.Rows.Count, 3).End(xlUp).Offset(1, 0)
' Set targetRange as the first cell in column C without conditional formatting
' under the last cell in column C with no value.
Do Until targetRange.FormatConditions.Count = 0
Set targetRange = targetRange.Offset(1, 0)
Loop
' Copy range C22:M22.
copySheet.Range("C22:M22").Copy
' Paste the copied range into targetRange.
targetRange.PasteSpecial xlPasteAll
' Lock pasteSheet to prevent further editing.
pasteSheet.Protect Password:="password"
' Get the loop count from Sheet3!A1
loopCount = Sheets("Sheet3").Range("A1").Value
' Loop from 1 to the specified loop count
For i = 1 To loopCount
' # **"I CANT WORK OUT WHAT TO PUT HERE"**
Next i
' Unprotect and unhide printSheet.
printSheet.Visible = xlSheetVisible
printSheet.Unprotect Password:="password"
' Open print preview for the specified range.
printSheet.Range("B1:N46").PrintPreview
' Hide and protect printSheet again.
printSheet.Protect Password:="password"
printSheet.Visible = xlSheetHidden
' Turn off the cut-copy mode.
Application.CutCopyMode = False
' Turn on screen updating.
Application.ScreenUpdating = True
End Sub
Ive tried copying this as the task I want to repeat in to "I CANT WORK OUT WHAT TO PUT HERE"
' Set variables.
Set copySheet = Worksheets("Sheet5")
Set pasteSheet = Worksheets("Sheet6")
' Unlock pasteSheet to allow editing.
pasteSheet.Unprotect Password:="password"
' Set targetRange as the last cell in column C with a value.
Set targetRange = pasteSheet.Cells(pasteSheet.Rows.Count, 3).End(xlUp).Offset(1, 0)
' Set targetRange as the first cell in column C without conditional formatting
' under the last cell in column C with no value.
Do Until targetRange.FormatConditions.Count = 0
Set targetRange = targetRange.Offset(1, 0)
Loop
' Copy range C22:M22.
copySheet.Range("C22:M22").Copy
' Paste the copied range into targetRange.
targetRange.PasteSpecial xlPasteAll
' Lock pasteSheet to prevent further editing.
pasteSheet.Protect Password:="password"
I managed to solve my question. I was trying to adapt on old macro and mis-understanding the loop function.
I have removed the following from the above code:
AND I have amended the loop to be:
The above now removes redundant code and allows my desired function to run "X" amount of times with "X" being the value of Sheet3A1. Note as per comments I forgot to define printSheet which I have now amended in my original question.