Find function in VBA

Rsull

New Member
Joined
May 31, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am currently trying to use the find function to search a column and stop at anything greater than a certain value. I haven't had any luck using the find function for anything but the equals operator. Is there a way to use it or something similar with greater than operator?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I want to search through a column and find the first cell that has a value greater than something, using macros. I don't think a filter would be a good solution.
A filter ?​
 
Upvote 0
Welcome to the board!

I agree. I don't think a filter is particularly helpful for what you want to do.

Try this code (just change the column reference and value you want to look for):
VBA Code:
Sub MyFindMacro()

    Dim col As String
    Dim lr As Long
    Dim r As Long
    Dim val As Double
    Dim fr As Long
    
'***Indicate which column to look in
    col = "A"
    
'***Indicate value to look for
    val = 7
    
'   Find last row with data in desired column
    lr = Cells(Rows.Count, col).End(xlUp).Row
    
'   Loop through all rows (starting on row 2) and search for value
    For r = 2 To lr
'       See if value in cell is greater than or equal to search value
        If Cells(r, col) >= val Then
            fr = r
            Exit For
        End If
    Next r
    
'   See if value found
    If fr > 0 Then
        Cells(fr, col).Activate
        MsgBox "Value found in cell " & Cells(fr, col).Address
    Else
        MsgBox "No value found"
    End If
    
End Sub
 
Upvote 0
Solution
Welcome to the board!

I agree. I don't think a filter is particularly helpful for what you want to do.

Try this code (just change the column reference and value you want to look for):
VBA Code:
Sub MyFindMacro()

    Dim col As String
    Dim lr As Long
    Dim r As Long
    Dim val As Double
    Dim fr As Long
   
'***Indicate which column to look in
    col = "A"
   
'***Indicate value to look for
    val = 7
   
'   Find last row with data in desired column
    lr = Cells(Rows.Count, col).End(xlUp).Row
   
'   Loop through all rows (starting on row 2) and search for value
    For r = 2 To lr
'       See if value in cell is greater than or equal to search value
        If Cells(r, col) >= val Then
            fr = r
            Exit For
        End If
    Next r
   
'   See if value found
    If fr > 0 Then
        Cells(fr, col).Activate
        MsgBox "Value found in cell " & Cells(fr, col).Address
    Else
        MsgBox "No value found"
    End If
   
End Sub
Thanks a lot! I used your code and it worked very well!
 
Upvote 0
You are welcome.
Glad it works for you!
:)
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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