+ Reply to Thread
Results 1 to 5 of 5

Iterate through merged range

Hybrid View

  1. #1
    Registered User
    Join Date
    11-20-2008
    Location
    Washington
    Posts
    16

    Iterate through merged range

    I'm trying to iterate through a range of merged cells, but each cell in the merged row is being iterated through and I want it to go from row to row.
    This what I have, but the subscript goes out of range since is looking at the cells within the merged rows.

    Public Sub PostSupers(ByRef rngSupers As Range, ByRef strarySupers() As String)
        
        Dim intSuper As Integer
        Dim Super As Range
        intSuper = 0
        For Each Super In rngSupers
    
            Super.Value = strarySupers(intSuper)
            intSuper = intSuper + 1
    
        Next Super
    
    End Sub
    Last edited by MrFoxar; 09-02-2009 at 12:35 AM. Reason: solved

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Iterate through merged range

    Add this to the loooooong LIST of reasons to NOT use merged cells.

    Excel provides the "Center across Selection" option so cells look merged but are not, so why use anything else?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Iterate through merged range

    Merging cells is a universally bad idea when there is any related downstream processing.

    Maybe this will work (completely untested):
    Public Sub PostSupers(ByRef rSup As Range, ByRef asSup() As String)
        Dim i           As Long
        Dim j           As Long
    
        If rSup.Areas.Count > 1 Then Stop    ' the code doesn't accommodate this
        i = 1
        j = LBound(asSup)
    
        Do
            rSup(i) = asSup(j)
            i = i + 1
            j = j + 1
            Do While rSup(i).MergeArea.Address = rSup(i - 1).MergeArea.Address And i < rSup.Count
                i = i + 1
            Loop
        Loop While i <= rSup.Count And j <= UBound(asSup)
    End Sub
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    11-20-2008
    Location
    Washington
    Posts
    16

    Re: Iterate through merged range

    Thanks,

    I kept the code as is and change the format to center accross selection.
    That was a bit of a challenge, but it worked out great. Thanks for the tip. I imagine that I will do it that way from this point on.

    The challenge was that I had to insert a "spacer" column between the areas which were being populated; otherwise the data would center accross selections in cases were there was no data.

    I'm sure a more graphical explanation would better explain what I am doing, but suffice it to say the problem has been solved, and once again, thank you.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Iterate through merged range

    Glad to hear it. If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)

+ 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