Referencing Merged Cells in VBA
According to the help files for MergeArea, it...
"Returns a Range object that represents the merged range
containing the specified cell. If the specified cell isn't in a
merged range, this property returns the specified cell"
So your code would seem safe to use. I would point out that this part of
your expression...
MergeArea(1, 1)
can be written like this...
MergeArea(1)
if you want to save a couple of characters. Cells within Ranges can be
references as you originally showed (which is really a short cut for using
the Cells property) or as a single array of cells where the progression is
across the rows, moving down to the next row after reaching the last column
in the range. One caveat with the one-dimensional referencing is it does not
automatically end at the last cell in the range. If your range were this...
Set Rng = Range("D5:H9")
then Rng(1) would reference D5, Rng(2) would reference E5, Rng(5) would
reference H5, Rng(6) would reference D6, Rng(25) would reference H9 (the
last cell in the range; however, Rng(26) would reference D10 and so on. The
only way to stop at the last cell is to set a reference of Rng(Rng.Count) as
the upper limit of a loop or test for it in an If..Then test. With that
said, I find the one-dimensional range referencing to be quite useful at
times.
--
Rick (MVP - Excel)
"Ron Rosenfeld" wrote in message
...
I am in a situation where I am having to reference merged cells in VBA.
The
source of the data is not something I would have any control over or input
into.
This is a code sample I am using:
r1.Offset(0, 1).Value * r1.Offset(0, 2).MergeArea(1, 1).Value
r1.offset(0,1) is not a merged cell but r1.offset(0,2) should usually be a
merged cell. I suppose it might not be on occasion, but that is not the
case
yet.
Any caveats or other suggestions would be appreciated.
In my limited testing, .MergeArea(1,1) returns the data, whether or not
the
cell is a merged cell.
Thanks.
--ron
|