Need help with Print to PDF macros in Excel

JayNExcel

New Member
Joined
Dec 15, 2019
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I have written a macros to take a worksheet and print it to PDF based on the index number in cell AQ1. The macros works fine, however; I want to be able to print to pdf for over 100 index numbers. I don't want to have to list out every number where I have Array. I was testing the macros using Array(1,2,3,4) and it works fine but now i want to replace that and find a way to tell it to print from 1-120 without typing each number in Array. How else can I do this? Here is the macros that I have:

Sub PDFTest()

Dim vars As Variant, i As Long
vars = Array(1, 2, 3, 4)
For i = LBound(vars) To UBound(vars)
[AQ1] = vars(i)

s = Range ("B2").Value
ChDir ActiveWorkbook.Path & "\"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=
s, Quality:=xlQualityStandard, From:=1, To:=1, IncludeDocProperites_
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

Next i
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this

VBA Code:
Sub PDFTest()
  Dim i As Long, s As String
  ChDir ActiveWorkbook.Path & "\"
  For i = 1 To 120
    [AQ1] = i
    s = Range("B2").Value
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=s, _
      Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, From:=1, To:=1, OpenAfterPublish:=False
  Next i
End Sub
 
Upvote 0
BTW, welcome to the board!

I'm glad to help you. Thanks for the feedback. :cool:
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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