Match entire cell contents using VBA

Ragavhere

New Member
Joined
Jan 7, 2011
Messages
4
Hi,

i get the error "Object variable or with block variable not set" with the below code.

Code:
sub findcolumn()
Dim FindWeek as string
FindWeek = "Wk1"
Range("A2:BL2").Select
Selection.find(What:=FindWeek, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
nCurrentWeekCol = ActiveCell.Column
Endsub
when i change LookAt:=xlWhole as LookAt:=xlPart i dont get the error.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It's because there no entry in the range A2:BL2 that only says "Wk1", but it is part of a text string i.e. "Wk1 Sales"
 
Upvote 0
Or you may have a cell with Wk1 with a trailing space after. It would "look" like Wk1 but would not match with xlWhole because of the trailing space.
 
Upvote 0
How do i modify the code such that the entire cell content is matched

Change the 'FindWeek' variable to reflect the entire string (cell content) you're searching for, not just 'Wk1' i.e.

Code:
FindWeek = "Wk1 Sales"

in my example.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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