Excel lookup for a value

majuranus

New Member
Joined
Jun 1, 2011
Messages
1
Hello everybody,

I am trying to make a function that will pop up an input window where the user should give a string. Then i want to activate (select) the row(s) that the string was found. The search takes place only in col A

this is some code i found, and mess with it. Problem is that it only gets the first result. Any help would be gratefull :)
Code:
Dim sMessage As String
Dim sTitle As String
Dim sResponse As String
sMessage = "Enter string"
sTitle = "Search using name column"
FindString = InputBox(sMessage, sTitle)
If Trim(FindString) <> "" Then
        With Sheets("sheet1").Range("A:A")
            Set Rng = Cells.Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, False
            Else
                MsgBox "no data found. sorry"
            End If
        End With
    End If
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,

Welcome to the board

The macro below was adapted from
http://www.rondebruin.nl/delete.htm
for select, instead of delete, the rows that contain a specific value entered by the user

Code:
Sub Union_Example()
    'Mr Excel Forum - Thread 554036
    'Adapted from [URL]http://www.rondebruin.nl/delete.htm[/URL]
    Dim Firstrow As Long, Lastrow As Long, Lrow As Long
    Dim CalcMode As Long, ViewMode As Long
    Dim sMessage As String, sTitle As String, FindString As String
    Dim rng As Range
 
    sMessage = "Enter string"
    sTitle = "Search using name column"
    FindString = InputBox(sMessage, sTitle)
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
 
    'Using Sheet1 but you can replace this with
    'Sheets("MySheet")if you want
    With Sheets("Sheet1")
        'We select the sheet so we can change the window view
        .Select
 
        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
 
        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False
 
        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
 
        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1
            'We check the values in the A column in this example
            With .Cells(Lrow, "A")
                If Not IsError(.Value) Then
                    If .Value = FindString Then
                        'This will select each row with the Value = FindString
                        'in Column A, case sensitive.
                        If rng Is Nothing Then
                            Set rng = .Cells
                        Else
                            Set rng = Application.Union(rng, .Cells)
                        End If
                    End If
                End If
            End With
        Next Lrow
    End With
 
    If Not rng Is Nothing Then
        'Select all rows in one time
        rng.Select
    Else
        'No data found
        MsgBox "no data found. sorry"
    End If
 
    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
End Sub

HTH

M.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

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