Forum Discussion
PRINT TO PDF VBA
I have an excel that calculates business position for 481 branch locations. There are certain number of branches under each Regional Office (RO/RO Name). User can select the branch by entering the DP Code of the branch or by selecting from drop down list. Data is being fetched from 24 different sheets in the main profile sheet. I need help with creating macro so that the profile sheet can be printed in pdf format in a specific location. The pdf should be saved in the format "DP Code - Branch Name" for each branch or the profile of the entire 481 branches can be exported in a single file with sequence as per RO Name. Also it would be extremely helpful if the pdf export can be done by selecting the RO Name into a single file or creating different macros for each RO so that profiles of all branches under the RO can be saved in a single file.
I have reviewed the file you provided. It seems like you have multiple sheets containing data for different branches and ROs, with the main data being fetched from the "HRM" tab.
To address your requirement, we will need to adjust the VBA code to properly loop through the data and generate PDFs based on either RO or certain DP codes.
For the first code you provided, we will modify it to use the data from the "HRM" tab to fetch branch details. We will also add functionality to filter based on either RO or specific DP codes.
Here is the modified code:
Vba code is untested, please backup your file.
Sub ExportProfilesToPDF() Dim wsProfile As Worksheet Dim wsHRM As Worksheet Dim lastRow As Long Dim branchName As String Dim dpCode As String Dim savePath As String Dim filterRO As String Dim filterDP As String ' Define the main profile sheet Set wsProfile = ThisWorkbook.Sheets("ProfileSheet") Set wsHRM = ThisWorkbook.Sheets("HRM") ' Define filter values (RO Name or DP Code) filterRO = "RO Name" ' Change this to the desired RO Name or leave as "RO Name" to export all ROs filterDP = "DP Code" ' Change this to the desired DP Code or leave as "DP Code" to export all branches ' Loop through each row in the HRM sheet lastRow = wsHRM.Cells(wsHRM.Rows.Count, "A").End(xlUp).Row For i = 2 To lastRow ' Assuming row 1 is header If (filterRO = "RO Name" Or wsHRM.Cells(i, "A").Value = filterRO) And (filterDP = "DP Code" Or wsHRM.Cells(i, "B").Value = filterDP) Then branchName = wsHRM.Cells(i, "C").Value ' Assuming branch name is in column C dpCode = wsHRM.Cells(i, "B").Value ' Assuming DP code is in column B savePath = "Your\Desired\Save\Path\" ' Change this to your desired save path ' Print the profile sheet to PDF wsProfile.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=savePath & dpCode & " - " & branchName & ".pdf", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End If Next i MsgBox "PDFs have been created successfully.", vbInformation End Sub
For the second code you provided, it seems like you are already generating PDFs based on RO, DP Code, and branch. If you want to filter based on RO or specific DP codes, you can modify the filter values accordingly before running the macro.
The second code can be modified, to meet your requirements.
Let us adjust it to include filtering based on either RO or specific DP codes.
Here is the modified code:
Vba code is untested, please backup your file.
Public Sub Create_PDFs() Dim destinationFolder As String Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range Dim branch As Range Dim RO As Range Dim filterRO As String Dim filterDP As String destinationFolder = "C:\Inward" ' Change this to your desired folder 'destinationFolder = "C:\path\to\folder\" 'Or specific folder If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\" ' Cell containing data validation in-cell dropdown Set RO = Worksheets("format").Range("G3") Set dataValidationCell = Worksheets("format").Range("A3") Set branch = Worksheets("format").Range("B3") ' Source of data validation list Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1) ' Define filter values (RO Name or DP Code) filterRO = "RO Name" ' Change this to the desired RO Name or leave as "RO Name" to export all ROs filterDP = "DP Code" ' Change this to the desired DP Code or leave as "DP Code" to export all branches ' Create PDF for each data validation value For Each dvValueCell In dataValidationListSource If (filterRO = "RO Name" Or RO.Value = filterRO) And (filterDP = "DP Code" Or branch.Value = filterDP) Then With Worksheets("format").Range("A1:T65") ' Adjust the range as per your requirement .ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & RO.Value & " - " & dvValueCell.Value & " - " & branch.Value & ".pdf", _ Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End With End If Next End Sub
This code will generate PDFs based on the filter values you set for RO and DP codes. If the filter values are set to "RO Name" or "DP Code", it will export all ROs or all branches respectively. Otherwise, it will export PDFs only for the specified RO or DP code.
Make sure to adjust the file paths and ranges as per your actual workbook structure.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- NikolinoDEGold Contributor
Creating a VBA macro to print the profile sheet to PDF format for each branch or each Regional Office (RO) can be achieved. Below is a basic structure for such a macro:
Vba code is untested and is a example, please backup your file before you using the code.
Sub ExportProfilesToPDF() Dim wsProfile As Worksheet Dim wsRO As Worksheet Dim lastRow As Long Dim roName As String Dim branchName As String Dim dpCode As String Dim savePath As String ' Define the main profile sheet Set wsProfile = ThisWorkbook.Sheets("ProfileSheet") ' Loop through each row in the profile sheet For Each wsRO In ThisWorkbook.Worksheets If wsRO.Name <> "ProfileSheet" Then roName = wsRO.Name savePath = "Your\Desired\Save\Path\" ' Change this to your desired save path ' Determine the last row in the worksheet lastRow = wsRO.Cells(wsRO.Rows.Count, "A").End(xlUp).Row ' Loop through each row in the worksheet For i = 2 To lastRow ' Assuming row 1 is header branchName = wsRO.Cells(i, 2).Value ' Assuming branch name is in column B dpCode = wsRO.Cells(i, 1).Value ' Assuming DP code is in column A ' Print the profile sheet to PDF wsProfile.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=savePath & dpCode & " - " & branchName & ".pdf", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False Next i End If Next wsRO MsgBox "PDFs have been created successfully.", vbInformation End Sub
This macro will iterate through each worksheet in the workbook, except for the "ProfileSheet" worksheet. For each worksheet (representing a Regional Office), it will loop through the rows to get the branch details (DP Code and Branch Name) and print the profile sheet to a PDF file named as "DP Code - Branch Name.pdf" in the specified save path.
You can modify this macro further to include options for selecting RO Name or exporting profiles of all branches under the same RO into a single file. Additionally, you can add error handling and more customization based on your specific requirements. The text, steps and code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- visheshkothaCopper ContributorThat did not work, mostly because of the different worksheets. The details like manager name RO Name etc are being fetched from the HRM Tab
Here is the link for the file. Kindly check if you can help me out.
https://drive.google.com/file/d/15bM1T31K9eEbvtOL64D2i63pQ_60Zjtf/view?usp=sharing - visheshkothaCopper ContributorFound the code online and with some modifications I was able to get it to print as per requirement. But still one critical filter is missing to print either RO wise or certain DP Codes.
Public Sub Create_PDFs()
Dim destinationFolder As String
Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range
Dim branch As Range
Dim RO As Range
destinationFolder = "C:\Inward" 'Same folder as workbook containing this macro
'destinationFolder = "C:\path\to\folder\" 'Or specific folder
If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
'Cell containing data validation in-cell dropdown
Set RO = Worksheets("format").Range("G3")
Set dataValidationCell = Worksheets("format").Range("A3")
Set branch = Worksheets("format").Range("B3")
'Source of data validation list
Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1)
'Create PDF for each data validation value
For Each dvValueCell In dataValidationListSource
dataValidationCell.Value = dvValueCell.Value
With dataValidationCell.Worksheet.Range("A1:T65")
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & RO.Value & " - " & dvValueCell.Value & " - " & branch.Value & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
Next
End Sub- NikolinoDEGold Contributor
I have reviewed the file you provided. It seems like you have multiple sheets containing data for different branches and ROs, with the main data being fetched from the "HRM" tab.
To address your requirement, we will need to adjust the VBA code to properly loop through the data and generate PDFs based on either RO or certain DP codes.
For the first code you provided, we will modify it to use the data from the "HRM" tab to fetch branch details. We will also add functionality to filter based on either RO or specific DP codes.
Here is the modified code:
Vba code is untested, please backup your file.
Sub ExportProfilesToPDF() Dim wsProfile As Worksheet Dim wsHRM As Worksheet Dim lastRow As Long Dim branchName As String Dim dpCode As String Dim savePath As String Dim filterRO As String Dim filterDP As String ' Define the main profile sheet Set wsProfile = ThisWorkbook.Sheets("ProfileSheet") Set wsHRM = ThisWorkbook.Sheets("HRM") ' Define filter values (RO Name or DP Code) filterRO = "RO Name" ' Change this to the desired RO Name or leave as "RO Name" to export all ROs filterDP = "DP Code" ' Change this to the desired DP Code or leave as "DP Code" to export all branches ' Loop through each row in the HRM sheet lastRow = wsHRM.Cells(wsHRM.Rows.Count, "A").End(xlUp).Row For i = 2 To lastRow ' Assuming row 1 is header If (filterRO = "RO Name" Or wsHRM.Cells(i, "A").Value = filterRO) And (filterDP = "DP Code" Or wsHRM.Cells(i, "B").Value = filterDP) Then branchName = wsHRM.Cells(i, "C").Value ' Assuming branch name is in column C dpCode = wsHRM.Cells(i, "B").Value ' Assuming DP code is in column B savePath = "Your\Desired\Save\Path\" ' Change this to your desired save path ' Print the profile sheet to PDF wsProfile.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=savePath & dpCode & " - " & branchName & ".pdf", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End If Next i MsgBox "PDFs have been created successfully.", vbInformation End Sub
For the second code you provided, it seems like you are already generating PDFs based on RO, DP Code, and branch. If you want to filter based on RO or specific DP codes, you can modify the filter values accordingly before running the macro.
The second code can be modified, to meet your requirements.
Let us adjust it to include filtering based on either RO or specific DP codes.
Here is the modified code:
Vba code is untested, please backup your file.
Public Sub Create_PDFs() Dim destinationFolder As String Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range Dim branch As Range Dim RO As Range Dim filterRO As String Dim filterDP As String destinationFolder = "C:\Inward" ' Change this to your desired folder 'destinationFolder = "C:\path\to\folder\" 'Or specific folder If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\" ' Cell containing data validation in-cell dropdown Set RO = Worksheets("format").Range("G3") Set dataValidationCell = Worksheets("format").Range("A3") Set branch = Worksheets("format").Range("B3") ' Source of data validation list Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1) ' Define filter values (RO Name or DP Code) filterRO = "RO Name" ' Change this to the desired RO Name or leave as "RO Name" to export all ROs filterDP = "DP Code" ' Change this to the desired DP Code or leave as "DP Code" to export all branches ' Create PDF for each data validation value For Each dvValueCell In dataValidationListSource If (filterRO = "RO Name" Or RO.Value = filterRO) And (filterDP = "DP Code" Or branch.Value = filterDP) Then With Worksheets("format").Range("A1:T65") ' Adjust the range as per your requirement .ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & RO.Value & " - " & dvValueCell.Value & " - " & branch.Value & ".pdf", _ Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End With End If Next End Sub
This code will generate PDFs based on the filter values you set for RO and DP codes. If the filter values are set to "RO Name" or "DP Code", it will export all ROs or all branches respectively. Otherwise, it will export PDFs only for the specified RO or DP code.
Make sure to adjust the file paths and ranges as per your actual workbook structure.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.