creating multiple PDFs by looping through number list

davidryoung

Board Regular
Joined
Feb 7, 2005
Messages
110
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
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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.
 
Upvote 0
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:
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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