VBA match function doesn't work when a cell is merged

blue333

Board Regular
Joined
Mar 19, 2009
Messages
64
Hi Everyone

I am working on a macro where I need find the column number which contains a particular text. However the noted cell is merged with others. I prefer to not unmerge the cells. Is there a way to make the match function work with merged cells? If match is not the way to go then I am open to other ways. In the end, I just need to find the column number where the text is located.

First attempt:

target_location=application.match("YTD",sheets(counter1).rows("7:7"),0)

Second attempt:

target_location=application.match("YTD",sheets(counter1).rows("7:8"),0)

The merged cell is rows 7 and 8 so I thought second attempt would work. No luck. The compiler ran an error at this line.

Any thoughts?
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Is counter1 a worksheet variable? or just the name of the worksheet? If it is just the name then you need quotes around counter1 ("counter1") 7:7 should work even if there are merged rows
 
Last edited:
Upvote 0
This worked for me

Code:
Dim target_location As Long, counter1 As Long
counter1 = 9
target_location = Application.Match("YTD", Sheets(counter1).Rows("7:7"), 0)
MsgBox target_location

where in my 9th sheet (from left to right) F7 = YTD and F7 and F8 are merged

M.
 
Upvote 0
This worked for me

Code:
Dim target_location As Long, counter1 As Long
counter1 = 9
target_location = Application.Match("YTD", Sheets(counter1).Rows("7:7"), 0)
MsgBox target_location

where in my 9th sheet (from left to right) F7 = YTD and F7 and F8 are merged

M.

its complaining of a type mismatch.

I have two identical sheets which are being cycled through. one has merged cells (V7 and V8) while other does not. the compiler stopped at the merged one.

Any thoughts?
 
Upvote 0
Is really V7 = YTD (not V8), no spaces, no extraneous characters?
To check try in an empty cell
=V7="YTD"

M.
 
Upvote 0
Hi,
Application.Match returns an error if value not found which presumably, is why your code is errors.

You can manage this by using the IsError function however, your variable target_location will need to be declared as Variant data type.

Code:
     Dim target_location As Variant
    target_location = Application.Match("YTD", Sheets(counter1).Rows("7:7"), 0)
    If Not IsError(target_location) Then
    
' match found do stuff
    
    End If

Where Match is found you can coerce your variable target_location to correct data type using a type conversion function


Code:
MsgBox CLng(target_location)


Hope Helpful


Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,567
Members
449,171
Latest member
jominadeo

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