VBA to remove empty elements from array

Tanyaann1995

Board Regular
Joined
Mar 24, 2021
Messages
62
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have created a userform where the user can pick which sheets they want to get printed by clicking on checkboxes. Once the user chooses which sheets they want to print, the code should print out only those sheets using an array. I have designed the code in such a way that if the user picks only certain sheets, the other array values become blank. I need to remove these empty values from the array to print out only the required sheets. Please help.

VBA Code:
Sub print()

Dim PDFfile As String
    Dim wksAllSheets() As Variant
    Dim wksSheet1 As Worksheet
    Dim wb As Workbook
    Dim arr1, arr2, arr3, arr4
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.CheckBox5.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"
wb.Sheets(wksAllSheets).Select
Set wksSheet1 = wb.Sheets("Cover")
    wksSheet1.ExportAsFixedFormat _
              Type:=xlTypePDF, _
              Filename:=PDFfile, _
              Quality:=xlQualityStandard, _
              IncludeDocProperties:=True, _
              IgnorePrintAreas:=False, _
              OpenAfterPublish:=False
End If
wb.Worksheets(1).Select
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

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