VBA to create bulk PDF for selected ID

MMM_84

New Member
Joined
Jan 13, 2021
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I have a tab with all Employee data and another tab with a template that I want to be PDF saved.
Smth similar to payslip. However, I want my VBA to give me a possibility to pick Employees from ID 1 to ID 10 (or ID 15).
I have the following VBA,


Sub PDF_All()

Sheets("Employee ALL").Select
Vrw = ActiveSheet.Range("CF10").End(xlDown).Row

For i = 10 To Vrw

Sheets("Employee ALL").Select
vemployeeID = ActiveSheet.Range("CF" & i)
Sheets("Template").Select
Set SaveRng = Range("Print_area")


Range("M2").Value = vemployeeID
pdfname = Range("P3") & Range("A8") & "_2024"
vpath = "Z:\...."
Sheets("Template").Select
SaveRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=path & pdfname & ".pdf"

Next

MsgBox "Completed"

End Sub


but not sure where I should put, that I want to select Employees from to....
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Assuming that the rest of your code works, is this what you are looking for:
Rich (BB code):
Sub PDF_All()

Dim n1 As Long, n2 As Long

'Prompt user for first and last employee numbers
n1 = InputBox("Enter starting employee number")
n2 = InputBox("Enter ending employee number")

Sheets("Employee ALL").Select

For i = n1 To n2

    Sheets("Employee ALL").Select
    vemployeeID = ActiveSheet.Range("CF" & i)
    Sheets("Template").Select
    Set SaveRng = Range("Print_area")


    Range("M2").Value = vemployeeID
    pdfname = Range("P3") & Range("A8") & "_2024"
    vpath = "Z:\...."
    Sheets("Template").Select
    SaveRng.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Path & pdfname & ".pdf"

Next

MsgBox "Completed"

End Sub
 
Upvote 0
Assuming that the rest of your code works, is this what you are looking for:
Rich (BB code):
Sub PDF_All()

Dim n1 As Long, n2 As Long

'Prompt user for first and last employee numbers
n1 = InputBox("Enter starting employee number")
n2 = InputBox("Enter ending employee number")

Sheets("Employee ALL").Select

For i = n1 To n2

    Sheets("Employee ALL").Select
    vemployeeID = ActiveSheet.Range("CF" & i)
    Sheets("Template").Select
    Set SaveRng = Range("Print_area")


    Range("M2").Value = vemployeeID
    pdfname = Range("P3") & Range("A8") & "_2024"
    vpath = "Z:\...."
    Sheets("Template").Select
    SaveRng.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Path & pdfname & ".pdf"

Next

MsgBox "Completed"

End Sub
Thank you Joe4, it works but it works Enter starting Employee number is in fact a raw number....and not the Employee number (Employee ID) in column CF
 
Upvote 0
I am not sure what you mean, but that is not surprising, see I have no idea what your data looks like.
Can you post a sample of your data (being sure to remove any sensitive data first)? And then tell us what you are entering in for the two criteria, and what your expected result is, and what you are getting instead?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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