tzallas
New Member
- Joined
- May 18, 2019
- Messages
- 8
With some great help from the forum community here, I have managed to use a macro that runs through a dropdown list and exports a sheet to a pdf in a user determined location
Huge thanks to @Yongle
See this thrread for details: https://www.mrexcel.com/forum/excel...ropdown-list-export-into-pdf.html#post5279704
I have about 25 separate workbooks that would need to have the same maro in them, so it would be beneficial to be able to run the same macro on multiple workbooks at a click of a button
I have been able to smash together the below, based on information from here: https://stackoverflow.com/questions/44282502/run-excel-macro-on-multiple-workbooks-files
My VBA skills are below novice
questions:
I don't know if I am making my life easier or more complicated with this one, any feedback is greatly appreciated nonetheless.
Thank you
Huge thanks to @Yongle
See this thrread for details: https://www.mrexcel.com/forum/excel...ropdown-list-export-into-pdf.html#post5279704
I have about 25 separate workbooks that would need to have the same maro in them, so it would be beneficial to be able to run the same macro on multiple workbooks at a click of a button
I have been able to smash together the below, based on information from here: https://stackoverflow.com/questions/44282502/run-excel-macro-on-multiple-workbooks-files
My VBA skills are below novice
questions:
- I assume the workbooks that would be affected by the code would also have to be macro enabled (.xlsm)?
- Where would tha "master code" need to be stored? do I create a blank workbook with the just the module in there and link it to a button, open that and start macro? or does it have to reside in one of the affected workbooks? or in all? could we trigger it with some sort of desktop icon?
- as I understand it if I run the code as is, it will prompt for a destionation folder for export for each workbook. Which is good. Would there be a way to have it prompt once for an export location for all affected workbooks in the current macro loop? or even give the option for one universal destination or destination per workbook? just to avoid having the user to standby and interact if they don't need to.
- Also would it be possible to make the macro close affected workbooks after completion (without affecting/saving)? it seems to leave them open currently
I don't know if I am making my life easier or more complicated with this one, any feedback is greatly appreciated nonetheless.
Thank you
Code:
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.xls*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
Const NamedRangeName = "Tool_Named_Range"
Const SheetName = "Uni-MAG"
Const CellWithDropdown = "B5"
Const PrintRange = "A4:L41"
Const DefaultFolder = "C:\Users\Public\Desktop\"
Dim fPath As String, choice As Range, fName As String
'Prompt user to select folder
MsgBox "Select folder for PDF Export", vbOKOnly + vbInformation, "PDF Export"
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select destination folder for PDF export"
.InitialFileName = DefaultFolder
If .Show = -1 Then fPath = .SelectedItems(1)
End With
'rest of code
If fPath <> "" Then
With Sheets(SheetName)
For Each choice In Range(NamedRangeName)
.Range(CellWithDropdown) = choice
'choice is the dropdown list contents
fName = choice & ".pdf"
.Range(PrintRange).ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPath & "\" & fName
Next
End With
Else
'Prompt user that no file was selected
MsgBox "No folder was selected", vbOKOnly + vbInformation, "No Folder Selection"
End If
End With
xFileName = Dir
Loop
End If
End Sub