Tanyaann1995
Board Regular
- Joined
- Mar 24, 2021
- Messages
- 62
- Office Version
- 2016
- Platform
- Windows
Hi,
I have prepared below code to print only certain sheets in a workbook based on the ticked checkboxes in a userform. But, I'm getting a "Run Time Error 9: Subscript out of Range" in the below highlighted purple line. Please advise if there is any way to fix this error.
I have prepared below code to print only certain sheets in a workbook based on the ticked checkboxes in a userform. But, I'm getting a "Run Time Error 9: Subscript out of Range" in the below highlighted purple line. Please advise if there is any way to fix this error.
VBA Code:
Function uniqueArr(ParamArray myArr() As Variant) As Variant()
Dim dict As Object
Dim V As Variant, W As Variant
Dim I As Long
Set dict = CreateObject("Scripting.Dictionary")
For Each V In myArr 'loop through each myArr
For Each W In V 'loop through the contents of each myArr
If Not dict.exists(W) Then dict.Add W, W
Next W
Next V
uniqueArr = dict.keys
End Function
Sub pdf()
Dim PDFfile As String
Dim CurrentPrinterNe As String, PrinterNe As String
Dim wksAllSheets As Variant
Dim wksSheet1 As Worksheet
Dim wb As Workbook
Dim arr1, arr2, arr3, arr4, arr5
Set wb = ActiveWorkbook
UserForm5.Show
If UserForm5.CheckBox1.Value = True Then
arr1 = Array("Cover")
Else
arr1 = Array("")
End If
If UserForm5.CheckBox2.Value = True Then
arr2 = Array("Revision")
Else
arr2 = Array("")
End If
If UserForm5.CheckBox3.Value = True Then
arr3 = Array("Emerson COMMERCIAL OFFER")
Else
arr3 = Array("")
End If
If UserForm5.CheckBox4.Value = True Then
arr4 = Array("Technical Offer")
Else
arr4 = Array("")
End If
wksAllSheets = uniqueArr(arr1, arr2, arr3, arr4)
PDFfile = wb.path & "\" & wb.Worksheets(1).Range("F12").Value & ".pdf"
[COLOR=rgb(85, 57, 130)]wb.Sheets(wksAllSheets).Select[/COLOR]
Set wksSheet1 = wb.Sheets("Cover")
wksSheet1.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=PDFfile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
wb.Worksheets(1).Select
End Sub