[VBA] Print up to a certain number

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
54
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Good morning,
I have the below code that cycles through a drop down, prints to PDF, and output saves into a specific folder. However, I want to be able to put a number in a cell, J4 for example, so that it only prints the first X number to PDF then stops. For example, if there are 100 items in the drop down to print, but J4 shows the number 50. It will only cycle and print the first 50 to PDF and stop after. Can you help?

VBA Code:
Public Sub Create_PDFs()

    Dim DesktopFolder As String, sfs As String
    Dim r As Range
   
    sfs = "Ready to Mail"
    With CreateObject("WScript.Shell")
    DesktopFolder = .SpecialFolders("Desktop") & "\"
    sfs = DesktopFolder & sfs
    .Run "cmd /c md " & """" & sfs & """", 0, True
   
    For Each r In Range(Range("K3").Validation.Formula1)
        Range("K3").Value = r.Value
        Sheets("Letter").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=sfs & "\" & r & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
    End With
    
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
793
Office Version
  1. 365
Platform
  1. Windows
Would this work for you ?

Just after your Dim statements add:-
VBA Code:
    Dim prnMax As Long
    Dim prnCntr As Long
    
    prnMax = Range("J4").Value
    prnCntr = 0

Just before your Next statement (end of the For), add:-
VBA Code:
            prnCntr = prnCntr + 1
            If prnCntr = prnMax Then Exit For
 
Solution

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
54
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Would this work for you ?

Just after your Dim statements add:-
VBA Code:
    Dim prnMax As Long
    Dim prnCntr As Long
   
    prnMax = Range("J4").Value
    prnCntr = 0

Just before your Next statement (end of the For), add:-
VBA Code:
            prnCntr = prnCntr + 1
            If prnCntr = prnMax Then Exit For
Alex, Thank you! This works perfectly.

Below is the full code;
VBA Code:
Public Sub Create_PDFs()

    Dim DesktopFolder As String, sfs As String
    Dim r As Range
    Dim prnMax As Long
    Dim prnCntr As Long
    
    prnMax = Range("J4").Value
    prnCntr = 0
   
    sfs = "Ready to Mail"
    With CreateObject("WScript.Shell")
    DesktopFolder = .SpecialFolders("Desktop") & "\"
    sfs = DesktopFolder & sfs
    .Run "cmd /c md " & """" & sfs & """", 0, True
   
    For Each r In Range(Range("K3").Validation.Formula1)
        Range("K3").Value = r.Value
        Sheets("Letter").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=sfs & "\" & r & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    prnCntr = prnCntr + 1
    If prnCntr = prnMax Then Exit For
    Next
    End With
    
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,132,646
Messages
5,654,561
Members
418,140
Latest member
ahepple86

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