Adapting Paste for A4 loop

Infine

Board Regular
Joined
Oct 16, 2019
Messages
74
Hello,

I have create a template which is adapted for an A4 paper area. I want to put it below each other on an Excel sheet and then print to PDF. It worked for MONTHS but all suddenly the area for A4 (settings) changed and my Range numbers were not adapting the A4 paper.

So basically:

..................
.I PASTE HERE
.
.
..................

and then from nowhere after several months the pasting is here:

..................
.
.I PASTE HERE
.
..................

and when this happens, the loop gets 1-2 steps down and the template I made is mixing on the A4 area.

Does anyone know why the area of the A4 changing? Nobody has changed anything with this Excel file nor script.

See my code below for how I paste it (I have updated it to a more simpler and math way, but I am still curious why it changed)

VBA Code:
If pdfcount = 2 Then
Worksheets("PDF").Range("A52").Select
ElseIf pdfcount = 3 Then
Worksheets("PDF").Range("A100").Select
ElseIf pdfcount = 4 Then
Worksheets("PDF").Range("A148").Select
ElseIf pdfcount = 5 Then
Worksheets("PDF").Range("A196").Select
ElseIf pdfcount = 6 Then
Worksheets("PDF").Range("A244").Select
ElseIf pdfcount = 7 Then
Worksheets("PDF").Range("A292").Select
ElseIf pdfcount = 8 Then
Worksheets("PDF").Range("A340").Select
ElseIf pdfcount = 9 Then
Worksheets("PDF").Range("A388").Select
ElseIf pdfcount = 10 Then
'......... And so on up to 20

End If
 

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
2,658
Office Version
  1. 365
Platform
  1. Windows
If you are happy to adapt code to fit yourself, you will find code that does exactly what you a doing including testing for file exists and giving a message box and allow you to change the filename here on the Contextures site: Excel Macro to Save Sheets As PDF look for the heading on that page "Macro 3 - No Prompt - File Check". If you need more help with it, it might worth posting it as a separate question.

In terms of the PDF file suddenly not fitting, most of the shift seems to relate to your page 1 (from page 2 on used to need a +4 on the row count and no longer does), the other pages have changed from 48 to 47. Consider using page breaks instead of row counts and perhaps hard coding Page Setup margins.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Infine

Board Regular
Joined
Oct 16, 2019
Messages
74
Hello,

So I used this code:

VBA Code:
Range(Cells(47 * (pdfcount - 1), 1), Cells(47 * (pdfcount - 1), 1)).Select
    ActiveSheet.PasteSpecial xlPasteColumnWidths ' THIS IS HOW I PASTED BEFORE TOO.
    ActiveSheet.PasteSpecial xlPasteAllMergingConditionalFormats
    ActiveSheet.Paste

and it worked fine. Until today. The printing Areas HEIGHT has changed and become much larger. So from 47 it is now 51.

What I wanna do, if possible; Can I put a PRINTING area +2 row above my SELECT?


So:
---------- (set printing area) + 2 row above where I pasted.
..................
.I PASTE HERE
.
.
..................
---------- (set printing area) + 2 row above where I pasted.
..................
.I PASTE HERE
.
.
..................
---------- (set printing area) + 2 row above where I pasted.
..................
.I PASTE HERE
.
.
..................


etc.

In that way no matter printing settings etc, the printing area will be the same. Is this possible and what is the code to do this Set printing row area?
 

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
2,658
Office Version
  1. 365
Platform
  1. Windows
Hello,

So I used this code:

VBA Code:
Range(Cells(47 * (pdfcount - 1), 1), Cells(47 * (pdfcount - 1), 1)).Select
    ActiveSheet.PasteSpecial xlPasteColumnWidths ' THIS IS HOW I PASTED BEFORE TOO.
    ActiveSheet.PasteSpecial xlPasteAllMergingConditionalFormats
    ActiveSheet.Paste

and it worked fine. Until today. The printing Areas HEIGHT has changed and become much larger. So from 47 it is now 51.

What I wanna do, if possible; Can I put a PRINTING area +2 row above my SELECT?


So:
---------- (set printing area) + 2 row above where I pasted.
..................
.I PASTE HERE
.
.
..................
---------- (set printing area) + 2 row above where I pasted.
..................
.I PASTE HERE
.
.
..................
---------- (set printing area) + 2 row above where I pasted.
..................
.I PASTE HERE
.
.
..................


etc.

In that way no matter printing settings etc, the printing area will be the same. Is this possible and what is the code to do this Set printing row area?

Do you want tot try adding this line before your code ?

VBA Code:
    Range(Cells(47 * (Pdfcount - 1), 1), Cells(47 * (Pdfcount - 1), 1)).PageBreak = xlPageBreakManual
 

Infine

Board Regular
Joined
Oct 16, 2019
Messages
74
Do you want tot try adding this line before your code ?

VBA Code:
    Range(Cells(47 * (Pdfcount - 1), 1), Cells(47 * (Pdfcount - 1), 1)).PageBreak = xlPageBreakManual


Exactly.

I will try this! Thanks. I will be back if it did not work.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,115
Messages
5,768,200
Members
425,459
Latest member
Danniey

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
Top