Results 1 to 6 of 6

How to get the number of rows or columns in a merged cell?

Threaded View

  1. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: How to get the number of rows or columns in a merged cell?

    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.

    =A1
    will return value of zaza

    =A2
    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:

    =RCX(zaza,0)
    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
    Last edited by DonkeyOte; 01-22-2010 at 07:06 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1