userform listbox add items but not if they are marked cancelled

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,056
Office Version
  1. 365
Platform
  1. Windows
When adding items to a listbox, how do i avoid ones marked "Do Not Use" (DNU). This is where I am picking up my listbox contents:

Code:
Private Sub FillResults()    Dim ws As Worksheet
    Dim rngFound As Range
    Dim strFirst As String
    Dim strSearch As String
    Dim strWSname As String
    Dim Excludedsheets As Variant, GetSearchArray As Variant
    Dim x As Variant
    Dim l As Long
   


    l = 1
     
    On Error GoTo ErrorHandle
    frmBookings.LbxExistBook.Clear
    strSearch = frmBookings.cboMembName.Value
    
    For Each ws In ActiveWorkbook.Sheets
    
        Excludedsheets = Array("Homepage", " ", "  ", "Trips", "Data", "First", "Last", "Members")
        x = Application.Match(ws.Name, Excludedsheets, 0)
        If IsError(x) Then
            l = l + 1


         
            Set rngFound = ws.Range("C:C").Find(strSearch, ws.Range("C4"), xlFormulas, xlWhole, xlByColumns)
            If Not rngFound Is Nothing Then
                strWSname = ws.Range("D2") & ", " & Format(ws.Range("g2"), "Ddd dd Mmm YYYY")
    
    
                With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With
   
            End If
        End If
    Next ws
    Exit Sub
ErrorHandle:
    If Err.Number = 9 Then
        MsgBox ("Error")
    Else
        MsgBox (Err.Description)
    End If                                       '
End Sub

If an item is marked DNU, this is stated in column A on each sheet.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
If I understand you correctly, maybe this:
Code:
                If rngFound.Offset(, -2).Value <> "Do Not Use" Then
                With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With
                End If
 
Upvote 0
If I understand you correctly, maybe this:
Code:
                If rngFound.Offset(, -2).Value <> "Do Not Use" Then
                With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With
                End If

I tried your suggestion above and got "Object variable or with block variable not set"

Code:
Set rngFound = ws.Range("C:C").Find(strSearch, ws.Range("C4"), xlFormulas, xlWhole, xlByColumns)
           '/// If Not rngFound Is Nothing Then
            If rngFound.Offset(, -2).Value <> "Do Not Use" Then
                strWSname = ws.Range("D2") & ", " & Format(ws.Range("g2"), "Ddd dd Mmm YYYY")
    
    
                With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With
   
            End If

Is that how you meant me to use it?
 
Last edited:
Upvote 0
No, just add 2 lines with blue (don't change anything else):

Code:
                   With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With


Code:
                [COLOR=#0000ff]If rngFound.Offset(, -2).Value <> "Do Not Use" Then[/COLOR]
                With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With
               [COLOR=#0000ff] End If[/COLOR]
 
Last edited:
Upvote 0
so have been playing around with it. it now looks like this:

Code:
Set rngFound = ws.Range("C:C").Find(strSearch, ws.Range("C4"), xlFormulas, xlWhole, xlByColumns)
            If Not rngFound Is Nothing And rngFound.Offset(, -2).Value <> "Do Not Use" Then
                strWSname = ws.Range("D2") & ", " & Format(ws.Range("g2"), "Ddd dd Mmm YYYY")
    
                With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With

the error occurs when looking at the first sheet. there is nothing to find on the first tab, rngFound is empty. It should just move onto the next tab instead of throwing an error.
 
Upvote 0
Did you read post 4?
Just add 2 lines with blue (don't change anything else):
Code:
                [COLOR=#0000ff]If rngFound.Offset(, -2).Value <> "Do Not Use" Then[/COLOR]
                With frmBookings.LbxExistBook
                    .AddItem strWSname
                End With
               [COLOR=#0000ff] End If[/COLOR]
 
  • Like
Reactions: ajm
Upvote 0
strange, I didn't see your second post (post 4) until this morning. i'll give it a go when I get into work. thanks for keeping with me. I tend to ramble my way through problems until I either get it resolved or someone on here points me in the right direction.
 
Upvote 0
thanks Akuini. works as it should now.
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,719
Members
452,995
Latest member
isldboy

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