Find All Instances of Value in a Range

keith0528

Active Member
Joined
Apr 23, 2009
Messages
250
Greetings All,

I have a range of data in column A and I want to identify all instances of a number in the range. The number is actually part of a string which is alpha numeric.

example:

147258 found in 7777
123456 found in 9999
123456 found in 9999
123456 found in 9999
654321 found in 8888
654321 found in 8888
654321 found in 8888

I have 123456 in a variable TicketNum and I want to identify all instances of it. In the above example the 3 lines in red should be selected. My current way is only finding 1 at a time. Here is my code:

Code:
TicketNum = Left(sVal, InStr(1, sVal, " ") - 1)

'find all instances of value (ticketnumber)
                            
 Set rngSearch = ActiveSheet.Range("A2:A" & LastRow)
 Set rngFound = rngSearch.Find(What:=TicketNum, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
rngFound.Select

once selected, these will be cut and paste to another tab.

thanks in advance
 

Some videos you may like

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,384
This uses the Autofilter feature.

Code:
TicketNum = Left(sVal, InStr(1, sVal, " ") - 1)

[color=green]'find all instances of value (ticketnumber)[/color]
Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:=TicketNum & "*"

Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
    Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
 

keith0528

Active Member
Joined
Apr 23, 2009
Messages
250
This uses the Autofilter feature.

Code:
TicketNum = Left(sVal, InStr(1, sVal, " ") - 1)

[COLOR=green]'find all instances of value (ticketnumber)[/COLOR]
Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:=TicketNum & "*"

Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
    Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)


Thanks so much AlphaFrog - That does exactly what I need.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,907
Messages
5,598,788
Members
414,259
Latest member
beetle12

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
Top