VBA Help

johns99

Board Regular
Joined
Jun 11, 2013
Messages
223
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm currently using the following VBA to save files from a list of employee IDs to PDF files. Currently, the coding is saving the files with the employee IDs, but I'd like to change this to the employee names. Does anyone know what I need to do to update the below?


'Declare variables
Dim ws As Worksheet
Dim rngID As Range
Dim rngListStart As Range
Dim rowsCount As Long
Dim i As Long
Dim pdffilepath As String
Dim tempPdfFilePath As String

'Stop screen updating during running
Application.ScreenUpdating = False


'Reference the Report Sheet
Set ws = ActiveWorkbook.Sheets("Sample Statement")

'Reference Employee ID cell
Set rngID = ws.Range("G5")


'Reference the start of the employee list
Set rngListStart = ws.Range("Q10")

'Refeence the pdfFilePath
pdffilepath = "W:\Long Term Incentive Awards\PAYOUTS\2013 OICP\2024 January RSU Vestings\US Employees\STATEMENTS\LTI Vesting Statement - [ID].pdf"

'Count the number of employee ID
rowsCount = rngListStart.CurrentRegion.Rows.Count - 1

For i = 1 To rowsCount


'Change the current employee ID
rngID.Value = rngListStart.Offset(i - 1, 0).Value

'Replace [ID] with the current employee ID
tempPdfFilePath = Replace(pdffilepath, "[ID]", rngID.Value)

'Create the PDFs
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=tempPdfFilePath

Next i

'Restart screen updating during running
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I'm assuming you have a list of employees with fields Id, FirstName, LastName or something like that.

This line is where you would get the First and Last Name instead of the current Id.

'Change the current employee ID
rngID.Value = rngListStart.Offset(i - 1, 0).Value

maybe:
Dim firstLastName as string

firstLastName = rngListStart.Offset(i - 1, 1).Value & " " & rngListStart.Offset(i - 2, 0).Value
 
Upvote 0
Thank you, would this compromise the ID being changed on the lookup?
 
Upvote 0
I'm assuming you want this: LTI Vesting Statement - [ID].pdf" to become LTI Vesting Statement - John Smith.pdf"

Correct?
 
Upvote 0
Yes, that's correct. However, isn't rngID.Value = rngListStart.Offset(i - 1, 0).Value what I need in the coding to replace the employee ID for the lookup? Currently, it's copying the IDs from the list on Q10 and replacing in G5.
 
Upvote 0
1705420850005.png
 
Upvote 0
So to explain, we're creating statements for employees, 48 total, everything looksup to cell G5. The coding copies the next eeid from the list and pastes in G5, then saves as PDF.

Hope this is more clear, thansk.
 
Upvote 0
Assuming your first and last names are somewhere around "C1", just change fullName to correct cell reference.

Public Sub PrintStockGrants()

Dim ws As Worksheet
Dim rngID As Range
Dim rngListStart As Range
Dim rowsCount As Long
Dim i As Long
Dim pdffilepath As String
Dim tempPdfFilePath As String

'Stop screen updating during running
Application.ScreenUpdating = False


'Reference the Report Sheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

'Reference Employee ID cell
Set rngID = ws.Range("G5")

'Reference the start of the employee list
Set rngListStart = ws.Range("Q10")

'Refeence the pdfFilePath
pdffilepath = "W:\Long Term Incentive Awards\PAYOUTS\2013 OICP\2024 January RSU Vestings\US Employees\STATEMENTS\LTI Vesting Statement - [ID].pdf"

'Count the number of employee ID
rowsCount = rngListStart.CurrentRegion.Rows.Count - 1

For i = 1 To rowsCount

'Change the current employee ID
rngID.Value = rngListStart.Offset(i - 1, 0).Value

'Replace [ID] with the current employee ID
' get the first/last name
Dim fullName As String

fullName = ws.Range("c1").Value & " " & ws.Range("d1").Value
tempPdfFilePath = Replace(pdffilepath, "[ID]", fullName)

'Create the PDFs
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=tempPdfFilePath

Next i

'Restart screen updating during running
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,151
Members
452,891
Latest member
JUSTOUTOFMYREACH

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