Search worksheet for with a changeable column search

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,843
Office Version
  1. 2007
Platform
  1. Windows
I currently use the following code.

I have a userform which has a TextBox where a value to be searched for is entered.
A command button is then pressed to search for that value.
Found values are then placed into the Listbox by their Row number.

Currently the code searches the whole worksheet & i end up with many values in the Listbox.
Because of the entered Row numbers im having to look at each one so im looking to make it a bit easier.

Now either the code is in need of changing OR maybe if possible be edited so a Textbox can be added to the existing userform where the user would enter a value to be searched & also enter the column to searched.

Example.
Currently the user would type FRANCIS
The code searches all the worksheet & the values are shown like so in the Listbox.
1
22
29
36
100
345
457

If the user could also type the column to be searched say column F then run the code to search.
The Listbox would look like,
100
457


Rich (BB code):
Private Sub ClearSearchField_Click()
    TextBox1.Value = ""
    ListBox1.Clear
    TextBox1.SetFocus
    Range("A6").Select
End Sub

Private Sub CloseForm_Click()
    Unload DatabaseSearch
End Sub

Private Sub FindTheValue_Click()
    Application.ScreenUpdating = False
    Dim fnd As Range, srcRng As Range, i As Long, sAddr As String, srcWS As Worksheet, dic As Object
    Set srcWS = Sheets("DATABASE")
    Set srcRng = srcWS.Range("B6", srcWS.Range("AC" & Rows.count).End(xlUp))
    Set dic = CreateObject("scripting.dictionary")
    If TextBox1.Value = "" Then
        MsgBox "PLEASE TYPE A VALUE TO SEARCH FOR", vbCritical + vbOKOnly, "SEARCH BOX IS EMPTY"
    Else
        Set fnd = srcRng.Find(Me.TextBox1.Value, LookIn:=xlValues, LookAt:=xlPart)
        If Not fnd Is Nothing Then
            sAddr = fnd.Address
            Do
                If Not dic.Exists(fnd.Row) Then
                    dic.Add fnd.Row, Nothing
                    ListBox1.AddItem fnd.Row
                End If
                Set fnd = srcRng.FindNext(fnd)
            Loop While fnd.Address <> sAddr
            sAddr = ""
        Else
            MsgBox "NOTHING TO MATCH THE SEARCH VALUE", vbCritical, "VALUE NOT FOUND MESSAGE"
            TextBox1.Value = ""
            TextBox1.SetFocus
        
        End If
    End If
    Application.ScreenUpdating = True

End Sub

Private Sub ListBox1_Click()
Range("A" & ListBox1.List(ListBox1.ListIndex, 0)).Select
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello,

From what i understand, you can add a TextBox which contains the column to look at, and then replace the address indicated in

VBA Code:
Set srcRng = srcWS.Range("B6", srcWS.Range("AC" & Rows.count).End(xlUp))

By, let's say the column is entered in the TextBox "TBCol" :

VBA Code:
If TBCol.Value <> "" Then ' if the TextBox is not empty
    Set srcRng = srcWS.Range(TBCol.Value & ":" TBCol.Value)
Else ' if the TextBox is empty, we use your previous range
    Set srcRng = srcWS.Range("B6", srcWS.Range("AC" & Rows.count).End(xlUp))
End If

Be careful it will raise an error if the given value is not a valid column name.
 
Upvote 0
See if this does what you want:
VBA Code:
Private Sub FindTheValue_Click()
    Application.ScreenUpdating = False
    Dim fnd As Range, srcRng As Range, i As Long, sAddr As String, srcWS As Worksheet, dic As Object, col As String
    Set srcWS = Sheets("DATABASE")
    Set dic = CreateObject("scripting.dictionary")
    If TextBox1.Value = "" Then
        MsgBox "PLEASE TYPE A VALUE TO SEARCH FOR.", vbCritical + vbOKOnly, "SEARCH BOX IS EMPTY"
        Exit Sub
    Else
        col = InputBox("ENTER THE LETTER OF THE COLUMN TO SEARCH.")
        If col = "" Then
            MsgBox ("NO COLUMN LETTER WAS ENTERED. PLEASE TRY AGAIN AND ENTER THE LETTER OF THE COLUMN TO SEARCH.")
            Unload Me
            Exit Sub
        End If
        Set srcRng = srcWS.Range(col & "6", srcWS.Range(col & Rows.Count).End(xlUp))
        Set fnd = srcRng.Find(Me.TextBox1.Value, LookIn:=xlValues, LookAt:=xlPart)
        If Not fnd Is Nothing Then
            sAddr = fnd.Address
            Do
                If Not dic.Exists(fnd.Row) Then
                    dic.Add fnd.Row, Nothing
                    ListBox1.AddItem fnd.Row
                End If
                Set fnd = srcRng.FindNext(fnd)
            Loop While fnd.Address <> sAddr
            sAddr = ""
        Else
            MsgBox "NOTHING TO MATCH THE SEARCH VALUE.  PLEASE ENTER A VALID SEARCH VALUE OR A VALID COLUMN LETTER.", vbCritical, "VALUE NOT FOUND MESSAGE"
            TextBox1.Value = ""
            TextBox1.SetFocus
        
        End If
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,225,617
Messages
6,186,017
Members
453,334
Latest member
Prakash Jha

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