How To Omit Specific Worksheets From ActiveX Combo Box List

bearwires

Board Regular
Joined
Mar 25, 2008
Messages
57
Office Version
  1. 365
Platform
  1. Windows
I have managed to wangle together this VBA code to select & print preview certain worksheets from the workbook. It works great, but I want to exclude certain sheets from the list.

I am hoping some VBA guru here can help me out please?

VBA Code:
Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String

 'ERROR HANDLING
 Dim errMsg As String
On Error GoTo errHandler
 errMsg = "You Need To Select RAMS & COSHH To Print"
errHandler:
    MsgBox errMsg
    Exit Sub
'
       With ActiveSheet.ListBoxSh
           
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve SheetArray(c)
                SheetArray(c) = .List(i)
                        c = c + 1
            End If
        Next i

        Sheets(SheetArray()).PrintPreview

    End With

End Sub

I want to exclude the following list of worksheets and make sure there are no blank items in the ActiveX combo box print list which is located on the Project Details worksheet:
  • Project Details
  • RA
  • Lists
  • COSHH Assessments

Any help will be much appreciated.

Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I have managed to wangle together this VBA code to select & print preview certain worksheets from the workbook. It works great, but I want to exclude certain sheets from the list.

I am hoping some VBA guru here can help me out please?

VBA Code:
Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String

 'ERROR HANDLING
 Dim errMsg As String
On Error GoTo errHandler
 errMsg = "You Need To Select RAMS & COSHH To Print"
errHandler:
    MsgBox errMsg
    Exit Sub
'
       With ActiveSheet.ListBoxSh
         
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve SheetArray(c)
                SheetArray(c) = .List(i)
                        c = c + 1
            End If
        Next i

        Sheets(SheetArray()).PrintPreview

    End With

End Sub

I want to exclude the following list of worksheets and make sure there are no blank items in the ActiveX combo box print list which is located on the Project Details worksheet:
  • Project Details
  • RA
  • Lists
  • COSHH Assessments

Any help will be much appreciated.

Thanks
Sorry, pasted the wrong version of the vba code.
This is the correct version I need assistance with:

VBA Code:
Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String
Dim errMsg As String
errMsg = "You Need To Select RAMS & COSHH To Print"

On Error GoTo errHandler


    With ActiveSheet.ListBoxSh

        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve SheetArray(c)
                SheetArray(c) = .List(i)
                c = c + 1
            End If
        Next i

        Sheets(SheetArray()).PrintPreview

    End With


'ERROR HANDLING

Done:
    Exit Sub

'
errHandler:
    MsgBox errMsg

End Sub
 
Upvote 0
Sorry, pasted the wrong version of the vba code.
This is the correct version I need assistance with:

VBA Code:
Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String
Dim errMsg As String
errMsg = "You Need To Select RAMS & COSHH To Print"

On Error GoTo errHandler


    With ActiveSheet.ListBoxSh

        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve SheetArray(c)
                SheetArray(c) = .List(i)
                c = c + 1
            End If
        Next i

        Sheets(SheetArray()).PrintPreview

    End With


'ERROR HANDLING

Done:
    Exit Sub

'
errHandler:
    MsgBox errMsg

End Sub
I got this to work by specifying actual worksheets I wanted included rather than trying to omit what I didnt want from the whole workbook.
This is for a combo box with specified worksheets listed which can be used for quick print previewing using a macro button.

This is the methodology:

In VBA editor, On Worksheet code module where Combo Box is Located:

VBA Code:
Private Sub Worksheet_Activate()
'
Dim Sh As Worksheet
Set ws = Sheets(Array("Sheet 1", "Sheet 2", "Sheet 3"))     [I]'Add  as many sheets as you want in this array to show in the combo box[/I]
'
Me.ListBoxSh.Clear
'
For Each Sh In ws
'For Each Sh In ThisWorkbook.ws

    Me.ListBoxSh.AddItem Sh.Name

Next Sh
'

End Sub

Then, I created a separate module and used this VBA code and allocated it to a Printer icon macro button:

VBA Code:
Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String
Dim errMsg As String
errMsg = "[I]Your Error Message Goes Here[/I]"

On Error GoTo errHandler


    With ActiveSheet.ListBoxSh



            For i = 0 To .ListCount - 1
                If .Selected(i) Then
                ReDim Preserve SheetArray(c)
                SheetArray(c) = .List(i)
                c = c + 1
            End If
        Next i

        Sheets(SheetArray()).PrintPreview

    End With


'ERROR HANDLING

Done:
    Exit Sub

'
errHandler:
    MsgBox errMsg

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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