code to print multiple sheets is printing too quickly

palaeontology

Active Member
Joined
May 12, 2017
Messages
444
Office Version
  1. 2016
Platform
  1. Windows
I have the following code that does several things.

Code:
Private Sub CommandButton3_Click()
Dim Addme As Range
Dim x As Integer
If IsEmpty(Sheets("PrintTemplate").Range("V49")) Then
    Set Addme = Sheets("PrintTemplate").Range("V49")
Else
    Set Addme = Sheets("PrintTemplate").Range("V" & Rows.Count).End(xlUp).Offset(1, 0)
End If
For x = 0 To Me.ListBox_1st_Class.ListCount - 1
If Me.ListBox_1st_Class.Selected(x) Then
        Addme = Me.ListBox_1st_Class.List(x)
        Addme.Offset(, 1).Value = Me.ListBox_1st_Class.List(x, 1)
        Set Addme = Addme.Offset(1, 0)
        End If
    Next x
For x = 0 To Me.ListBox_1st_Class.ListCount - 1
If Me.ListBox_1st_Class.Selected(x) Then Me.ListBox_1st_Class.Selected(x) = False
Next x


'###########
'Code2
' "Y49", Column "Y" &  "Me.ListBox_2nd_Class"


If IsEmpty(Sheets("PrintTemplate").Range("Y49")) Then
    Set Addme = Sheets("PrintTemplate").Range("Y49")
Else
    Set Addme = Sheets("PrintTemplate").Range("Y" & Rows.Count).End(xlUp).Offset(1, 0)
End If
For x = 0 To Me.ListBox_2nd_Class.ListCount - 1
If Me.ListBox_2nd_Class.Selected(x) Then
        Addme = Me.ListBox_2nd_Class.List(x)
        Addme.Offset(, 1).Value = Me.ListBox_2nd_Class.List(x, 1)
        Set Addme = Addme.Offset(1, 0)
        End If
    Next x
For x = 0 To Me.ListBox_2nd_Class.ListCount - 1
If Me.ListBox_2nd_Class.Selected(x) Then Me.ListBox_2nd_Class.Selected(x) = False
Next x




 
 
 'Copy Template Multiple Times and Rename them with names from a List
Dim ws As Worksheet, Ct As Long, c As Range
Set ws = Worksheets("Student Profile Template")
Application.ScreenUpdating = False
For Each c In Sheets("PrintTemplate").Range("AH49:AH100")
    If c.Value <> "" Then
        ws.Copy after:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value
        Ct = Ct + 1
    End If
Next c
Application.ScreenUpdating = True






'Print all Sheets named with a 4-digit number
For Each ws In ThisWorkbook.Worksheets
    If ws.Name Like "#####" Then
        ws.Range("P22:U22").Font.Color = vbWhite
        ws.Range("P22:U22").Interior.Color = vbWhite
        ws.PageSetup.Orientation = xlLandscape
        ws.PrintOut From:=1, To:=1
        ws.PageSetup.Orientation = xlLandscape
        ws.PrintOut From:=2, To:=2
    End If
Next ws




'Delete all Sheets named with a 4-digit number
  Application.DisplayAlerts = False
  For Each ws In Worksheets
    If ws.Name Like "#####" Then ws.Delete
  Next ws




'Clear student names from chosen list
Dim tbl As Range
Set tbl = Sheets("PrintTemplate").Range("V49:AF400")
tbl.ClearContents


End Sub

within that code, there is a code to create a copy of a template worksheet for each name in a list and name each of those new worksheets a particular cell value which happens to be a 4-digit code.

Then there's another code to print all sheets with a 4-digit number.

However, when the new sheets are made, it takes a little while for all the formulae on the new worksheet to retrieve the information they're designed to display.

Consequently, when the print code runs, some of the cells haven't finished updating, so the print isn't showing true data.

Is there a way to slow down how long it takes to print, after the sheets have been made ... perhaps 30 seconds should do it, just to be sure.

Very kind regards,

Chris

However,
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
try this

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Application.Wait Now +#12:00:01 AM#


or this

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)</code>Or, for 64-bit systems use:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)</code>Call it in your macro like so:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
Sleep
(1000) ' delay 1 second

Loop
End Sub</code></code>
 
Last edited:
Upvote 0
Hi Blauv,

thankyou, so much, for responding so fast.

Where, within the code, would I place this ...

Code:
Application.Wait Now +#12:00:01 AM#

and if I wanted 30 seconds, would I make it ...

Code:
Application.Wait Now +#12:00:30 AM#

Kind regards,

Chris
 
Last edited:
Upvote 0
Actually I think this one works alot better.


Place this at the top of the VBA Module.

PublicDeclare PtrSafe Sub Sleep Lib"kernel32"(ByVal dwMilliseconds As LongPtr)


and then this where ever you need the delay in your macro

Sleep (1000) ' delay 1 second

adjust the 1000 as needed

30 seconds = 30000


 
Last edited:
Upvote 0
I would guess here form the what youv'e said

'Copy Template Multiple Times and Rename them with names from a List
Dim ws As Worksheet, Ct As Long, c As Range
Set ws = Worksheets("Student Profile Template")
Application.ScreenUpdating = False
For Each c In Sheets("PrintTemplate").Range("AH49:AH100")
If c.Value <> "" Then
ws.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value
Ct = Ct + 1
End If
Next c
Application.ScreenUpdating = True



Sleep (1000) ' delay 1 second


'Print all Sheets named with a 4-digit number


Might need to move around and test.
 
Last edited:
Upvote 0
ok, definitely showing my vba ignorance now ...

when you say ...

Place this at the top of the VBA Module.


Code:
PublicDeclare PtrSafe Sub Sleep Lib"kernel32"(ByVal dwMilliseconds As LongPtr)


do you mean at the top of my long code, or somewhere else ?
 
Upvote 0
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)</code>
Private Sub CommandButton3_Click()</pre>
 
Upvote 0
I placed the PublicDeclare where you indicated, and it's showing up red ... is that meant to happen ?

I placed the line ...

Sleep (1000) ' delay 1 second

where you indicated, but I'm getting a Compile error: Sub or Function not defined, and it's highlighting that same line

Is there something I've forgotten to do ?

Kind regards,

Chris
 
Upvote 0
sorry try this one.

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)




Private Sub CommandButton3_Click()
Sleep (1000) ' delay 1 second


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,693
Messages
6,173,874
Members
452,536
Latest member
Chiz511

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top