[VBA] Print up to a certain number

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
98
Office Version
  1. 365
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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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
 
Upvote 0
Solution
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
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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