I don't think the range is named before you merge it that or either only the top left most cell of the range is named before merge - in either case you would get 1 as you outline.
Merged cells do not work quite as people think - in reality the content of the merged area generally resides only in the top left most cell of the merged area.
will return value of zaza
will return nothing (blank/0) assuming it (A2) was empty prior to being merged.. although part of the merged area it is itself devoid of content - the content exists only in A1
So ... my point was basically... if you had A1:D5 highlighted, then named, then merged - ROWS and COLUMNS would return the expected values.
I'm not sure, off hand, is there is a native function you can use to determine the address of a merged area if the area is named after the merge or only the top left cell is named before merge ... I think you would be looking at VBA
(things are also complicated of course by the fact that any given name can cover multiple non-contiguous areas - merged or not)
Function RCX(rngX As Range, bType As Byte) As Long
Dim rngArea As Range, rngUnion As Range, rngRC As Range
On Error Resume Next
For Each rngArea In rngX.Areas
Set rngRC = rngArea.MergeArea
Set rngRC = IIf(rngRC Is Nothing, rngArea, rngRC)
If bType Then
Set rngRC = rngRC.EntireColumn
Else
Set rngRC = rngRC.EntireRow
End If
If rngUnion Is Nothing Then
Set rngUnion = rngRC
Else
Set rngUnion = Union(rngUnion, ComplementLoop(rngRC, rngUnion))
End If
Set rngRC = Nothing
Next rngArea
If bType Then
For Each rngRC In rngUnion.Columns: RCX = RCX + 1: Next rngRC
Else
For Each rngRC In rngUnion.Rows: RCX = RCX + 1: Next rngRC
End If
End Function
'below Function c/o Andy Pope
Function ComplementLoop(rngA As Range, rngB As Range) As Range
Dim rngX As Range
Dim rngC As Range
For Each rngX In rngA
If Intersect(rngX, rngB) Is Nothing Then
If rngC Is Nothing Then
Set rngC = rngX
Else
Set rngC = Union(rngC, rngX)
End If
End If
Next
Set ComplementLoop = rngC
End Function
stored in a Module in VBE, called from a cell along the lines of:
where the first parameter is the range of interest and the second (0,1) stipulates whether you're returning Rows (0) or Columns (1) ... for a non-contiguous range like A6:C7, B12:C13 the above would return 4 for rows and 3 for columns.
I'm sure there are far more elegant and robust methods out there to do this kind of analysis though.
EDIT: code corrected - missing a release of rngRC in the Loop
Bookmarks