searching when extra spaces exists in value

abenitez77

Board Regular
Joined
Dec 30, 2004
Messages
149
I am using vba to search for searching for certain key words/phrases in excel spreadsheets. Sometimes a phrase may have extra spaces in between words
i.e. "Forecast O&A $ per Unit"

When I search i use "Forecast O&A $ per Unit" (only one space between each word). In my spreadsheet "Forecast", "Unit" and other phrases may appear in other cells with other words. How can i search what I am looking for and get a hit even when there are extra spaces in value ?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I am using vba to search for searching for certain key words/phrases in excel spreadsheets. Sometimes a phrase may have extra spaces in between words
i.e. "Forecast O&A $ per Unit"

When I search i use "Forecast O&A $ per Unit" (only one space between each word). In my spreadsheet "Forecast", "Unit" and other phrases may appear in other cells with other words. How can i search what I am looking for and get a hit even when there are extra spaces in value ?
How are you doing the search? It might be helpful to see your code. When posting your code, put [/code] after the last line and put
Code:
[/COLOR][/B] before the first line.
 
Last edited:
Upvote 0
How are you doing the search? It might be helpful to see your code. When posting your code, put [/code] after the last line and put
Code:
[/COLOR][/B] before the first line.[/QUOTE]


[code]


Set FoundCell = SearchRange.Find(what:=FindWhat, _
            after:=LastCell, _
            LookIn:=LookIn, _
            LookAt:=XLookAt, _
            SearchOrder:=SearchOrder, _
            MatchCase:=MatchCase)
 
Upvote 0
Rich (BB code):
Set FoundCell = SearchRange.Find(what:=FindWhat, _
            after:=LastCell, _
            LookIn:=LookIn, _
            LookAt:=XLookAt, _
            SearchOrder:=SearchOrder, _
            MatchCase:=MatchCase)
I was afraid you did it that way (as opposed to doing a cell by cell comparison). Whether this solution will work 100% of the time depends on what possible values can be in SearchRange. As long as no other value in SearchRange can have all of the individual words "Forecast", "O&A", "$
, "per" and "Unit", in that exact order with or without other text between them, then replacing FindWhat with Replace(FindWhat, " ", "*") should work.
 
Upvote 0
I was afraid you did it that way (as opposed to doing a cell by cell comparison). Whether this solution will work 100% of the time depends on what possible values can be in SearchRange. As long as no other value in SearchRange can have all of the individual words "Forecast", "O&A", "$
, "per" and "Unit", in that exact order with or without other text between them, then replacing FindWhat with Replace(FindWhat, " ", "*") should work.

Thanks!, looks like it should work. I will give this a shot.
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,695
Members
449,117
Latest member
Aaagu

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