+ Reply to Thread
Results 1 to 4 of 4

UserForm Listbox Add Worksheets with If Check

Hybrid View

  1. #1
    Registered User
    Join Date
    11-07-2012
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    46

    UserForm Listbox Add Worksheets with If Check

    Hi working to activate my userform listbox with a list of all worksheets except certain named ones. This is what I have so far...

    Private Sub UserForm_Activate()
        Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
            
            If ws.Name <> "SheetName1" Or "Sheet2" Or "This_Sheet Then
    
                ListBox.AddItem (ws.Name)
            End If
    Next ws
    
        
    End Sub
    I'm getting an type mismatch error on "If ws.Name <> "SheetName1" Or "Sheet2" Or "This_Sheet Then"

    How do I specify which sheets to exclude?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: UserForm Listbox Add Worksheets with If Check

    Try this.
    For Each ws In ThisWorkbook.Worksheets
        Select Case ws.Name
             Case "SheetName1", "Sheet2", "This_Sheet"
                   ' do nothing
             Case Else
                   ' add worksheet
                  Listbox1.AddItem ws.Name
        End Select
    Next ws
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    11-07-2012
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: UserForm Listbox Add Worksheets with If Check

    thank u. that works. is it possible to do that in an If statement though? why didn't the original one work?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: UserForm Listbox Add Worksheets with If Check

    This is what the If would look like.
    If ws.Name<> "SheetName1" And ws.Name <>"Sheet2" And ws.Name<>"This_Sheet" Then

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1