VBA to search range and return value

nicolehalliday

Board Regular
Joined
May 19, 2010
Messages
56
Hello,

Thank you in advance for help with this problem! :)

I have spent quite a bit of time searching the forums for an answer and I've come up with this so far:

Code:
Sub vba()

Dim rng As Range
Application.ScreenUpdating = False

For Each rng In Range("F3:DG3")
    If rng.Value > Cells("A3").Value Then
        Cells(3, 3).Select
        Exit Sub
    End If
Next rng
Application.ScreenUpdating = True
End Sub

So basically I want my macro to search the range F3:DG3 and check if there is any value in the cell range that exceeds the value in cell A3. If there is a larger value in the range, I'd like it to enter "YES" in cell C3 (I was just using select to see if it would even work, which it doesn't). I've tried this a number of ways (vlookup, countif, etc.) but I can't get any of them to work when it's comparing a range to a cell, rather than a cell to a cell. I am quite inexperienced at VBA and excel so if anyone has a simple solution to this I would be extremely grateful!!!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hello,

Thank you in advance for help with this problem! :)

I have spent quite a bit of time searching the forums for an answer and I've come up with this so far:

Code:
Sub vba()

Dim rng As Range
Application.ScreenUpdating = False

For Each rng In Range("F3:DG3")
    If rng.Value > Cells("A3").Value Then
        Cells(3, 3).Select
        Exit Sub
    End If
Next rng
Application.ScreenUpdating = True
End Sub

So basically I want my macro to search the range F3:DG3 and check if there is any value in the cell range that exceeds the value in cell A3. If there is a larger value in the range, I'd like it to enter "YES" in cell C3 (I was just using select to see if it would even work, which it doesn't). I've tried this a number of ways (vlookup, countif, etc.) but I can't get any of them to work when it's comparing a range to a cell, rather than a cell to a cell. I am quite inexperienced at VBA and excel so if anyone has a simple solution to this I would be extremely grateful!!!

From what I see your code works (except I made one change). But be aware that once it finds the first instance where the value in the range exceeds the value in A3 it will exit sub? Perhaps you want it too something else like maybe highlight the cells?

Code:
Dim rng As Range
Application.ScreenUpdating = False

For Each rng In Range("F3:DG3")
    If rng.Value > Range("A3").Value Then
        Cells(3, 3).Value = "yes"
        Exit Sub
    End If
Next rng
Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,224,579
Messages
6,179,656
Members
452,934
Latest member
mm1t1

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