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!
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