when target.address is a range

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
I made a cell as a button so when the cell is clicked, a piece of code is run. The code looks like the following:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$O$1" Then
do something...

It worked fine. I then merged cells N1, O1, P1 but the code no longer works. I tried something like:

If Target.Address = range("N1:P1")

It did not work.

What should I do to make it work? Thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Merged cells and VBA really don't work together but maybe

If Target.Address = "$N$1"
 
Upvote 0
The address property looks for a string, not a Range...


If Target.Address = "$N$1:$P$1" Then
 
Upvote 0
Try checking the target address for $N$1:$P$1.
 
Upvote 0
The reason for the above suggestions, is that a merged cell doesn't actually become one cell. It just makes multiple cells behave as if it is one. The value is always contained in the upper left cell of the merged range.

So, in this case, if you are selecting or working with the merged range N1:P1, you are actually selecting 3 cells at the same time.
 
Upvote 0
Hi

If O1 is part of a group of merged cells you can try:

Code:
If Target.Address = Range("O1").MergeArea.Address Then
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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