PDA

View Full Version : Solved: Code to search for a Value in a Range



snowdyce
06-25-2013, 10:58 AM
Hey Experts,

I have run into a speed bump on some VBA code, which I think there is a simple solution I cannot find. I have a list of variables (1 thru 7). I want to check a range(Column) to see if a value of "1" is in that range. I do not need to save the value or care if it repeats, just if it appears at least once. I tried using a For formula that checked each Row, but it repeated each time it found a 1 in the range. I just want it to stop the search if it finds it, and then do action A if it does or aciton B if it doesnt. I also played with Find in ranges and I could not get it to work, andy ideas? Thanks. :banghead:

GarysStudent
06-25-2013, 11:42 AM
In this example we are searching column A, modify to suit:

Sub FindValue()
Dim rLook As Range, r As Range, WhatToFind As String, GotIt As Boolean
Set rLook = Intersect(ActiveSheet.UsedRange, Range("A:A"))
WhatToFind = "1"
GotIt = False
For Each r In rLook
If r.Text = WhatToFind Then
GotIt = True
Exit For
End If
Next

If GotIt Then
MsgBox "action a"
Else
MsgBox "action b"
End If
End Sub

snowdyce
06-25-2013, 11:48 AM
Great Thank you this works perfectly! :beerchug: