Have the code select the cell that relates to what was searched for

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,730
Office Version
  1. 2007
Platform
  1. Windows
Hi,
Im using the code below.

On the userform in TextBox1 i type a value to search for.
Pressing the commandbutton called FindThe Value it searches for all the matches to what i entered in TextBox1
ListBox1 then shows all the row numbers that have a match.
THE ABOVE ALL WORKS FINE.

Clicking each row number in Listbox1 selects the customer on my worksheet in column A until the required item is found.
Closing the userform leave the customer readily selected on my worksheet.

So here is my example.
I search for MIT 11
All the matches are now shown in ListBox1
I clicked each row number BUT as opposed selecting the customer in column A have the code select the cell that MIT 11 was found in.
So as opposed A123 being Mr Smith have the actual cell R123 selected.

Sometimes my worksheet columns are off the right hand side of the screen & im being shown Mr Simth in column A as opposed to the searched value of MIT 11 in R123

Please advise

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
    Set srcWS = Sheets("DATABASE")
    Set srcRng = srcWS.Range("C6", 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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,223,856
Messages
6,175,029
Members
452,606
Latest member
jkondrat14

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