Populate listbox but omit N/A value

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
The code in use is shown below.
The following code works fine but it also populates my listbox if the value consists of N/A
I start to search for N & there all listed.

How would i have the code to NOT populate the listbox & to ignore it if its value is N/A thus just populating it with any other N value


Rich (BB code):
Private Sub TextBoxKeyCode_Change()
Me.Label1.Visible = False
Me.Label2.Visible = False
Me.Label3.Visible = False
Me.Label4.Visible = False
TextBoxKeyCode = UCase(TextBoxKeyCode)
  Dim r As Range, f As Range, cell As String, added As Boolean
  Dim sh As Worksheet
  
  Set sh = Sheets("DATABASE")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "190;180;230;10"
    If TextBoxKeyCode.Value = "" Then Exit Sub
    Set r = Range("J5", Range("J" & Rows.Count).End(xlUp))
    Set f = r.Find(TextBoxKeyCode.Value, LookIn:=xlValues, LookAt:=xlPart)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        added = False
        For i = 0 To .ListCount - 1
          Select Case StrComp(.List(i), f.Value, vbTextCompare)
            Case 0, 1
              .AddItem f.Value, i
              .List(i, 1) = f.Offset(, -8).Value
              .List(i, 2) = f.Offset(, -9).Value
              .List(i, 3) = f.Row
              added = True
              Exit For
          End Select
        Next
        If added = False Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, -8).Value
          .List(.ListCount - 1, 2) = f.Offset(, -9).Value
          .List(.ListCount - 1, 3) = f.Row
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      TextBoxSearch = UCase(TextBoxSearch)
      .TopIndex = 0
      Else
      MsgBox "NO SUCH KEY CODE", vbCritical, "DATABASE SHEET ITEM SEARCH"
      TextBoxKeyCode.Value = ""
      TextBoxKeyCode.SetFocus
    End If
  End With
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try adding these 2 lines

VBA Code:
    If Not f Is Nothing Then
      cell = f.Address
      Do
        if f.value <> "N/A" then    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            added = False
            For i = 0 To .ListCount - 1
              Select Case StrComp(.List(i), f.Value, vbTextCompare)
                Case 0, 1
                  .AddItem f.Value, i
                  .List(i, 1) = f.Offset(, -8).Value
                  .List(i, 2) = f.Offset(, -9).Value
                  .List(i, 3) = f.Row
                  added = True
                  Exit For
              End Select
            Next
            If added = False Then
              .AddItem f.Value
              .List(.ListCount - 1, 1) = f.Offset(, -8).Value
              .List(.ListCount - 1, 2) = f.Offset(, -9).Value
              .List(.ListCount - 1, 3) = f.Row
            End If
        end if                      '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<    
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
 
Upvote 0
Solution

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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