VBA to search for multiple text strings in 2 columns

CraigKC

New Member
Joined
Dec 8, 2016
Messages
2
Hi,

I’ve been struggling to build a quick and speedy VBA (Excel 2013) to search for a match of 3 variables obtained from one worksheet that has 3k records, to a record in another worksheet that has over 800k records. I want to find the record that has a match for 1 variable in a cell in one column and a match for 2 variables in a cell in another column. Once I find the record, then I want to get data from other columns in that row and place on the other worksheet that I get my 3 variables. Many thanks in advance!!! Kelly

VarSub = “ARAPAHO EAST”
Sometimes lot is “LOT” or “LT”
VarLot1 = “LOT 22”
VarLot2 = “LT 22”
VarBlk = “BLK 8”
Returns match for Row 7 so I know where to get the other data for the record

Excel 2013 32 bit
W
X
1
LEGAL1LEGAL2
2
CAMP WISDOM WESTBLK D LT 4
3
ARAPAHO EAST 1 REVBLK 3 LOT 25
4
ARAPAHO EAST 1 REVBLK 4 LOT 12
5
ARAPAHO EAST 1 REVBLK 4 LOT 16
6
ARAPAHO EAST 2BBLK 7 LOT 5
7
ARAPAHO EAST 2BBLK 8 LOT 22
8
ARAPAHO EAST 2BBLK 8 LOT 37
9
ARAPAHO EAST 5BLK B LT 1
10
COUNTRY CLUB PARK ESTATESBLK D LOT 8

<tbody>
</tbody>
Sheet: Sheet1

<tbody>
</tbody>
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this. It has three input boxes one for subdivision, one for block and one for lot, a messasge box provides the row number for a match.
Code:
Sub findParcel()
Dim subd As String, blk As Variant, lot As Long, fn As Range, lr As Long, fAdr As String
lr = Sheets("Sheet1").Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
subd = UCase(InputBox("Enter the subdivision legal name, eg. 'ELEGANT ESTATES WEST'", "SUBDIVISION"))
blk = InputBox("Enter the Block numeral or letter only, no other text.", "BLOCK DESIGNTION")
lot = CLng(InputBox("Enter the Lot numeral only, no text.", "LOT NUMBER"))
    Set fn = Sheets("Sheet1").Range("W2:W" & lr).Find(subd, , xlValues, xlWhole)
        If Not fn Is Nothing Then
            fAdr = fn.Address
            Do
                If Trim(fn.Offset(, 1).Value) Like "BLK " & blk & " L* " & lot Then
                    MsgBox "Item found on Row " & fn.Row
                    Exit Do
                End If
                Set fn = Sheets("Sheet1").Range("W2:W" & lr).FindNext(fn)
            Loop While fn.Address <> fAdr
        End If
End Sub
 
Last edited:
Upvote 0
Thank JLGWhiz for posting your code. I had to add a wildcard character to the beginning of "BLK" and also at the end. I also moved the checking for the lot to a separate line since in my 800k plus rows of data, the space between the block identifier and lot could be more than just 1 space. Your code definitely was a step forward in solving my question. Your code is sort of like using the AutoFilter method. I was wondering if creating an array would be a faster method than using FIND? Here is the code that I modified above:

Code:
Sub findMatch()


Dim subd As String, blk As Variant, lot As Long, fn As Range, lr As Long, fAdr As String
lr = Sheets("Sheet1").Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
subd = UCase(InputBox("Enter the subdivision legal name, eg. 'ELEGANT ESTATES WEST'", "SUBDIVISION"))
blk = InputBox("Enter the Block numeral or letter only, no other text.", "BLOCK DESIGNTION")
lot = CLng(InputBox("Enter the Lot numeral only, no text.", "LOT NUMBER"))
    Set fn = Sheets("Sheet1").Range("W2:W" & lr).Find(subd, , xlValues, xlPart)
        If Not fn Is Nothing Then
            fAdr = fn.Address
            Do
                If Trim(fn.Offset(, 1).Value) Like "*BLK " & blk & "*" Then
                 If Trim(fn.Offset(, 1).Value) Like "*L* " & lot & "*" Then
                    MsgBox "Item found on Row " & fn.Row
                    Exit Do
                 End If
                End If
                Set fn = Sheets("Sheet1").Range("W2:W" & lr).FindNext(fn)
            Loop While fn.Address <> fAdr
        End If
End Sub

Again, I really appreciated your reply!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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