When you ask a question, it is recommended to frequently check it and try clarifying the comments asking for clarifications...
If I understood well your question, please test the next code. It assumes that the answer to my suppositions in the comment is yes. The code offers a browse window to select the folder where to export the chosen (sheetsNo
) number of sheets:
Sub ExportAsPDF()
Dim FolderPath As String, sheetsNo As Long, sh As Worksheet, arrSheets
Dim fldr As FileDialog, sItem As String, fileName As String, i As Long
sheetsNo = ActiveCell.value 'use there the sheet you need
ReDim arrSheets(sheetsNo - 1) 'redim the array to keep the sheets
'use a dialog to select the folder to export
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder where to export the pdf file"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
If sItem = "" Then Exit Sub 'if no folder selected the code stops
FolderPath = sItem
fileName = Replace(ThisWorkbook.Name, ".xlsm", ".pdf") 'use the workbook name, but changing its extension
For i = 1 To sheetsNo
arrSheets(i - 1) = Worksheets(i).Name 'put the sheets in an array
Next
Sheets(arrSheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:=FolderPath & "/" & fileName, _
openafterpublish:=False, ignoreprintareas:=False
MsgBox "All PDF's have been exported!"
End Sub
Please, test it and send some feedback.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…