Issue with Array of Sheets

liquidmetal24

New Member
Joined
Oct 29, 2021
Messages
3
Office Version
  1. 365
Platform
  1. 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.

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?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
That suggests that one of the sheets in the array doesn't exist in the workbook. Check that the spelling is correct & there are no leading/trailing spaces.
 
Upvote 0
Solution
That suggests that one of the sheets in the array doesn't exist in the workbook. Check that the spelling is correct & there are no leading/trailing spaces.

Well now I feel stupid. I apologize. There was a trailing space in the Sub that creates the "Paperwork" Sheets throwing things off. Thanks for your help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

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