+ Reply to Thread
Results 1 to 6 of 6

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

Hybrid View

  1. #1
    Registered User
    Join Date
    12-01-2007
    Posts
    42

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

    Hello,

    I would like, with a function for instance, to get the number of rows and/or columns of a merged cell.
    For instance the name of my merged cell is "zaza" which is the cells A1 to D3 merged.
    "zaza" represents a merged cell with 3 rows and 4 columns. How to get these noumbers?

    Thanks.

  2. #2
    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?

    Did you name the range before or after you merged it ?

    If you named A1:D3 "zaza" and then subsequently merged the cells you can obtain the Rows/Columns using:

    =ROWS(zaza)
    
    =COLUMNS(zaza)

  3. #3
    Registered User
    Join Date
    12-01-2007
    Posts
    42

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

    Hello DonkeyOte,

    The cell is named BEFORE.

  4. #4
    Registered User
    Join Date
    12-01-2007
    Posts
    42

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

    Unfortunately it does not work. If zaza is (A1:D5).Merge, I always get 1...

    I would like:

    - verticaly (Rows): 5
    - horizontaly (Columns): 4

  5. #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.

  6. #6
    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?

    Just to wrap this up...

    the UDF can be used with Merged Areas & Non-Merged Areas, Names and non-Names, Contiguous and non-Contiguous ranges...
    (obviously use with unnamed contiguous ranges would be a little pointless...)

    it should return the count of unique rows / columns contained within the specified range.

    Examples:

    =RCX(zaza,0)
    --> returns unique rows as used in named range (will use Merge address if exists)
    
    =RCX(zaza,1) 
    --> as above but returns unique columns
    
    =RCX((A1:A10,B1:B14,C2:F3,E12:G12,J2),0) 
    --> will return 14 given there are 14 unique rows
    
    =RCX((A1:A10,B1:B14,C2:F3,E12:G12,J2),1) 
    --> will return 8 as there are 8 unique columns [A:G,J]
    Note that if any of the above ranges were themselves part of a merged area the merge area range would supercede, eg: J2 was merged as J2:L2 then:

    =RCX((A1:A10,B1:B14,C2:F3,E12:G12,J2),1) 
    --> will return 10 as there are 10 unique columns [A:G,J:L] - K & L are included given J2 is merged with those columns.
    obviously you could add a further toggle re: unique/non-unique if so desired.
    Last edited by DonkeyOte; 01-22-2010 at 07:58 AM. Reason: J2 was referened as J2:J2 which was a bit pointless...

+ Reply to Thread

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