Print code occasionally messing up

palaeontology

Active Member
Joined
May 12, 2017
Messages
444
Office Version
  1. 2016
Platform
  1. Windows
I'm currently using the following code to copy a template sheet (called PrintTemplate) multiple times and rename them with names from a list found in a given range ...

Code:
Dim ws As Worksheet, Ct As Long, c As RangeSet 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

This code is working perfectly most of the time, but occasionally prints just one of the sheets multiple times and doesn't print the other sheets.

For example, if the range houses 4 different student names, the code should result in 4 copies of the template sheet being printed, each with one of the four student names, but occasionally what is produced is 4 sheets each with the same one name .. sometimes it will be the first name in the list, sometimes the second name, etc etc ... there's no consistency as to which name it gets 'hung up' on.

Following the code that produces named copies of the template sheet, I have 2 more codes .. one to cause the printing of these sheets, the other to delete the sheets just created ...

Code:
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
    End If
Next ws




  Application.DisplayAlerts = False
  For Each ws In Worksheets
    If ws.Name Like "#####" Then ws.Delete
  Next ws

Can anyone see why my code might not produce the correct desired result each time ?

Could the print require slowing down, to give time for each newly formed sheet to be read and printed ????

Kind regards,

Chris
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Maybe the sheets are not being named as a 5-digit number as the print code requires? Or have spurious characters left or right of the number?
 
Last edited:
Upvote 0
Thanks Shg, I looked into it and it doesn't seem to be that.

Any other thoughts ?

Do you think it might be trying to move too quickly between each print ?
 
Upvote 0
This code is working perfectly most of the time, but occasionally prints just one of the sheets multiple times and doesn't print the other sheets.

Or perhaps is printing single copies of multiple identical sheets?

I am guessing that if you have five students, say, you want to populate Worksheets("Student Profile Template") with each of their details, and print the results.

Your first bit of code creates five new sheets, but won't these be identical? Where are you customising for each individual student?

You can probably replace all this code with a simple loop, and avoid creating and deleting new sheets, if you give us more detail about what you're trying to do.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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