creating multiple PDFs by looping through number list

davidryoung

Board Regular
Joined
Feb 7, 2005
Messages
108
The code below is part of a macro being used to create a list of numbers and then loop through those numbers one at a time, place the number into cell I1 which is linked to numerous other sheets. Each of those sheets has a formula in cell Z1 that sets to "Yes" or "No" depending on if the current number in the list is on that sheet. If it is set to Yes, the macro is supposed to select each of those "yes" sheets and then create a PDF file, and then loops to the next number in the list.
There are two problems I haven't figured how to get around yet. First, the first loop through works correctly, but each subsequent loop keeps adding sheets that should not be marked "Yes". Second, it is also selecting the sheet the macro is running from, which I do not want it do.
Any suggestions are greatly appreciated!

Code:
'Find case numbers on worksheets and compile PDF

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim Casenumber As Variant
Dim mysheet As Object


'Create PDFs for Each case while selecting correct tabs
For Each Casenumber In Range("G2:G200")
Range("I1").Value = Casenumber
If Casenumber = "" Then GoTo Done

For Each mysheet In Sheets
If mysheet.Range("Z1") = "Yes" Then
mysheet.Select Replace:=False
End If
Next mysheet

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'create default name for saving file
strFile = ActiveSheet.Range("I1")
strPathFile = strPath & strFile

'user can enter name and select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

Next Casenumber
 

davidryoung

Board Regular
Joined
Feb 7, 2005
Messages
108
Re: creating multiple PDFs by looping through number list-- help needed

EDIT: Fixed the first issue by selecting another sheet at the end of the main loop. Now just need to keep it from adding the activesheet to the PDF files.
 

Swayzy

Board Regular
Joined
Mar 30, 2018
Messages
76
Re: creating multiple PDFs by looping through number list-- help needed

You are setting the sheet from which you run the macro as the variable wsA since that sheet is still ActiveX, so I guess that when you reference that variable in the export the sheet will remain in the declaration since it was active at declaration.

Simplest fix would be to Always activate the sheets you select so you deselect the current sheet you call the macro from.

In short thw only active sheets when you set wsA is the ones you want printed
 
Last edited:

davidryoung

Board Regular
Joined
Feb 7, 2005
Messages
108
Re: creating multiple PDFs by looping through number list-- help needed

Thanks for the reply Swayzy! I have tried some variations of what you are suggesting but with no luck. My issue is that the list of case numbers it is going through to create each PDF file is on that active sheet.
 

Swayzy

Board Regular
Joined
Mar 30, 2018
Messages
76
Re: creating multiple PDFs by looping through number list-- help needed

Then reference all ranges om that sheet explicitly so you dont have to go back to that sheet. So Range("G20:G40") becomes sheet1.Range("G20:G40") for example
 

Forum statistics

Threads
1,078,215
Messages
5,338,905
Members
399,267
Latest member
Danielle1017

Some videos you may like

This Week's Hot Topics

Top