Simple search macro

Chris_010101

Board Regular
Joined
Jul 24, 2017
Messages
187
Office Version
  1. 365
Platform
  1. Windows
Hello

1698261962617.png


In C1 i'd like to be able to type a name and when the search button is clicked, excel performs the search function.

I've tried recording a macro for this but it keeps going to an incorrect cell.

Is someone able to help with VB that I can assign to the button?

Many Thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Here is some code I wrote in 2015 for a search. The true condition for the IF is if the search fails, so change the those lines to whatever you want it to do if the name is not found.

VBA Code:
Private Sub SearchButton_Click()
    Application.EnableEvents = False
  
    If IsError(Application.Match(Range("C1").Value, ws.Range("B5:B100"), 0)) Then
            MsgBox "Account does not exist."
        Else
            ws.Range("B" & trgtRow).Select
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
This won't work I'm afraid.

I'm getting Runtime error 424 object required

1703003421389.png
 
Upvote 0
There is a worksheet variable in there, and it looks like I removed some of the other code, so if you use it as is, that error is not surprising. You'll have to declare the worksheet variable and set it to your sheet. In the code below, you would replace " your sheet name here " with the name of your worksheet, minus the extra spaces, but keep the quotation marks. Also, in my sheet the target value was in column B, so make sure you change the ranges to suit your data.

VBA Code:
Private Sub SearchButton_Click()
Dim ws As Worksheet
Set ws = Sheets(" your sheet name here ")
    Application.EnableEvents = False
 
    If IsError(Application.Match(Range("C1").Value, ws.Range("B5:B100"), 0)) Then
            MsgBox "Account does not exist."
        Else
            ws.Range("B" & trgtRow).Select
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
There is a worksheet variable in there, and it looks like I removed some of the other code, so if you use it as is, that error is not surprising. You'll have to declare the worksheet variable and set it to your sheet. In the code below, you would replace " your sheet name here " with the name of your worksheet, minus the extra spaces, but keep the quotation marks. Also, in my sheet the target value was in column B, so make sure you change the ranges to suit your data.

VBA Code:
Private Sub SearchButton_Click()
Dim ws As Worksheet
Set ws = Sheets(" your sheet name here ")
    Application.EnableEvents = False
 
    If IsError(Application.Match(Range("C1").Value, ws.Range("B5:B100"), 0)) Then
            MsgBox "Account does not exist."
        Else
            ws.Range("B" & trgtRow).Select
    End If
    Application.EnableEvents = True
End Sub
Hello,

I'm now getting this:

1703011069923.png


1703011086180.png
 
Upvote 0
Ah, another variable, my apologies.

VBA Code:
Private Sub SearchButton_Click()
Dim ws As Worksheet
Dim trgtRow As Long
Set ws = Sheets("2024")
    Application.EnableEvents = False
 
    If IsError(Application.Match(Range("C1").Value, ws.Range("A1:A100"), 0)) Then
            MsgBox "Account does not exist."
        Else
            trgtRow = Application.Match(Range("C1").Value, ws.Range("A1:A100"), 0))
            ws.Range("A" & trgtRow).Select
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 1
No need to apologise, I'm grateful for your help! :)

1703013450597.png



Edit: Sorted it by adding the missing opening parenthesis before "Application"

Thanks for your help! Much appreciated
 
Upvote 0
Yes, well, I should know better ;) Try this. There was an extra ")" on that line.

VBA Code:
Private Sub SearchButton_Click()
Dim ws As Worksheet
Dim trgtRow As Long
Set ws = Sheets("2024")
    Application.EnableEvents = False
 
    If IsError(Application.Match(ws.Range("C1").Value, ws.Range("A1:A100"), 0)) Then
            MsgBox "Account does not exist."
        Else
            trgtRow = Application.Match(ws.Range("C1").Value, ws.Range("A1:A100"), 0)
            ws.Range("A" & trgtRow).Select
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 1
Solution
Yes, well, I should know better ;) Try this. There was an extra ")" on that line.

VBA Code:
Private Sub SearchButton_Click()
Dim ws As Worksheet
Dim trgtRow As Long
Set ws = Sheets("2024")
    Application.EnableEvents = False
 
    If IsError(Application.Match(ws.Range("C1").Value, ws.Range("A1:A100"), 0)) Then
            MsgBox "Account does not exist."
        Else
            trgtRow = Application.Match(ws.Range("C1").Value, ws.Range("A1:A100"), 0)
            ws.Range("A" & trgtRow).Select
    End If
    Application.EnableEvents = True
End Sub
Thanks for your help! Much appreciated
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,146
Members
449,098
Latest member
Doanvanhieu

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