liquidmetal24
New Member
- Joined
- Oct 29, 2021
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
I currently am using an array of worksheets to lump them together and save them in a .pdf format. When I use the following code by itself everything works flawlessly.
However, the number of "Paperwork" sheets is dynamic each time the Workbook is used. So for instance on the next use the There might be 5 Paperwork Sheets starting at "Paperwork" and ending at "Paperwork_4". So I am using this full code currently:
The issue is on the line:
I get a "Subscript out of range" Error. Can anyone point out what I might be missing or why it is throwing the error?
VBA Code:
Dim ibl As Worksheet
Dim ibsArray As Variant
Dim fname As String
Dim location As String
Dim daypath As String
Dim path As String
ibsArray = Array("Inbound_Load", "Paperwork", "Paperwork_1")
fname = ibl.Range("$AF$7").Text
daypath = ibl.Range("$AF$8").Text
location = Environ("userprofile") & "\Burgess Enterprises Dropbox\Inbound\"
path = location & daypath
With CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(path) Then .CreateFolder path
End With
Sheets(ibsArray).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=path & "\" & fname & ".pdf"
However, the number of "Paperwork" sheets is dynamic each time the Workbook is used. So for instance on the next use the There might be 5 Paperwork Sheets starting at "Paperwork" and ending at "Paperwork_4". So I am using this full code currently:
VBA Code:
Dim p1c As Boolean
Dim p2c As Boolean
Dim p3c As Boolean
Dim p4c As Boolean
Dim p5c As Boolean
Dim sh As Worksheet
Dim ibl As Worksheet
Dim ibsArray As Variant
Dim fname As String
Dim location As String
Dim daypath As String
Dim path As String
Set ibl = Sheets("Sub_Inbound")
Set ibr = Sheets("Inbound_Report")
For Each sh In ThisWorkbook.Worksheets
If sh.Name Like "Paperwork_4" Then p5c = True: Exit For
Next
For Each sh In ThisWorkbook.Worksheets
If sh.Name Like "Paperwork_3" Then p4c = True: Exit For
Next
For Each sh In ThisWorkbook.Worksheets
If sh.Name Like "Paperwork_2" Then p3c = True: Exit For
Next
For Each sh In ThisWorkbook.Worksheets
If sh.Name Like "Paperwork_1" Then p2c = True: Exit For
Next
For Each sh In ThisWorkbook.Worksheets
If sh.Name Like "Paperwork" Then p1c = True: Exit For
Next
If p5c = True Then
ibsArray = Array("Inbound_Load", "Paperwork", "Paperwork_1", "Paperwork_2", "Paperwork_3", "Paperwork_4")
ElseIf p4c = True Then
ibsArray = Array("Inbound_Load", "Paperwork", "Paperwork_1", "Paperwork_2", "Paperwork_3")
ElseIf p3c = True Then
ibsArray = Array("Inbound_Load", "Paperwork", "Paperwork_1", "Paperwork_2")
ElseIf p2c = True Then
ibsArray = Array("Inbound_Load", "Paperwork", "Paperwork_1")
ElseIf p1c = True Then
ibsArray = Array("Inbound_Load", "Paperwork")
Else
End If
fname = ibl.Range("$AF$7").Text
daypath = ibl.Range("$AF$8").Text
location = Environ("userprofile") & "\Burgess Enterprises Dropbox\Inbound\"
path = location & daypath
With CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(path) Then .CreateFolder path
End With
Sheets(ibsArray).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=path & "\" & fname & ".pdf"
The issue is on the line:
VBA Code:
Sheets(ibsArray).Select
I get a "Subscript out of range" Error. Can anyone point out what I might be missing or why it is throwing the error?