Simple find command

kckay

Board Regular
Joined
Nov 8, 2010
Messages
134
I am trying to find the value "DO" in the B15:B40 range using VBA.

Should be straightforward, but I end up either not working it or running against the entire worksheet.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
This will find the first DO. If you want to find all you would have to make some type of loop.

Code:
Sub Find_DO()
    Range("B15:B40").Select
    Selection.Find(What:="DO", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
End Sub
 
Upvote 0
This tells you the row of first occurance of DO, but I'm still not clear on what your end goal is.

Code:
Sub Find_DO()
Dim MyRow
    Range("B15:B40").Select
    Selection.Find(What:="DO", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    MyRow = ActiveCell.Row
    MsgBox MyRow
End Sub
 
Upvote 0
Thank you very much for the assistance.

"DO" stands for Designated Operator and is a row of variable length. "DO" is the only common cell value of a series of records generated from an SQL server. Since the values are variable length and variable rows within records, now that I know the row, I can extract the data I need.
 
Upvote 0
Same general idea but
- you don't need to select the range to search it
- if you hold the result in a variable range then you can test whether it is found or not (whereas the code above will error if there is no "DO")

The code below searches for DO anywhere in a cell within B15:B40
If DO should be the entire cell contents, not just a partial match then use

Set rng1 = Range("b15:b40").Find(strFind, , xlValues, xlWhole)

Cheers

Dave


Code:
Sub FindMe()
    Dim strFind As String
    Dim rng1 As Range
    strFind = "DO"
    Set rng1 = Range("b15:b40").Find(strFind, , xlValues, xlPart)
    If Not rng1 Is Nothing Then
        MsgBox "row is " & rng1.Row
    Else
        MsgBox strFind & " not found"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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