+ Reply to Thread
Results 1 to 7 of 7

VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

Hybrid View

JamesGoulding85 VBA to return the column... 06-28-2013, 11:52 AM
tigeravatar Re: VBA to return the column... 06-28-2013, 12:07 PM
JasperD Re: VBA to return the column... 06-28-2013, 12:27 PM
JamesGoulding85 Re: VBA to return the column... 06-28-2013, 01:23 PM
tigeravatar Re: VBA to return the column... 06-28-2013, 01:27 PM
JamesGoulding85 Re: VBA to return the column... 06-28-2013, 01:31 PM
tigeravatar Re: VBA to return the column... 06-28-2013, 01:32 PM
  1. #1
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    83

    Red face VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

    I know this seems like a random one! but I have a really long merged cell called "Model Length". it can vary as to where it is on the sheet. what I want to do is return two variables one containing the column number of the start of the merged cell, the other to return the column number of the end of the merged cell.

    I currently have
    Sub FindMerged4()
        Dim c As Range
        Dim sMsg As String
    
        sMsg = ""
        For Each c In ActiveSheet.UsedRange
            If c.MergeCells Then
                If sMsg = "" Then
                    sMsg = "Merged worksheet cells:" & vbCr
                End If
                sMsg = sMsg & c.Address & vbCr
            End If
        Next
        If sMsg = "" Then
            sMsg = "No merged worksheet cells."
        End If
    
        MsgBox sMsg
    End Sub
    Which returns a list of all the cells in the merged cell but I cant figure out how to identify the start and finish points.

    Please help

    thanks

    Jim

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

    Jim,

    One way...
    Sub tgr()
        
        Dim rngFound As Range
        Dim arrMerged(1 To 65000, 1 To 3) As Variant
        Dim strFirst As String
        Dim MergedIndex As Long
        Dim i As Long
        
        With Application.FindFormat
            .Clear
            .MergeCells = True
        End With
        
        Set rngFound = Cells.Find(vbNullString, Cells(Rows.Count, Columns.Count), xlValues, xlPart, SearchFormat:=True)
        If Not rngFound Is Nothing Then
            strFirst = rngFound.Address
            Do
                MergedIndex = MergedIndex + 1
                arrMerged(MergedIndex, 1) = rngFound.MergeArea.Address
                arrMerged(MergedIndex, 2) = rngFound.Column
                arrMerged(MergedIndex, 3) = rngFound.Column + rngFound.MergeArea.Columns.Count - 1
                Set rngFound = Cells.Find(vbNullString, rngFound, xlValues, xlPart, SearchFormat:=True)
            Loop While rngFound.Address <> strFirst
            For i = 1 To MergedIndex
                MsgBox "Merged Cell Address:" & vbTab & arrMerged(i, 1) & Chr(10) & _
                       "Start Column Number:" & vbTab & arrMerged(i, 2) & Chr(10) & _
                       "End Column Number:  " & vbTab & arrMerged(i, 3)
            Next i
        End If
        
        Application.FindFormat.Clear
        Set rngFound = Nothing
        Erase arrMerged
        
    End Sub
    Last edited by tigeravatar; 06-28-2013 at 12:12 PM.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

    Jim,

    another way

    Sub TGIF()
    Dim cell As Range
    For Each cell In ActiveSheet.UsedRange
    If cell.MergeCells Then If cell.Address = cell.MergeArea.Cells(1, 1).Address Then MsgBox "Firts cell of merge area: " & cell.MergeArea.Cells(1, 1).Address & " -  Last cell of merge area: " & cell.MergeArea.Cells(cell.MergeArea.Rows.Count, cell.MergeArea.Columns.Count).Address
    Next cell
    
    End Sub
    Please click the * below if this helps
    Please click the * below if this helps

  4. #4
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    83

    Re: VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

    Awsome thanks tigeravatar and JasperD.

    is there anyway to actually specify the merged cell that contains the text "model life" only? as if there are other merged cells it returns those values as well?

    thanks

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

    So you would only want information regarding merged cells that contain the text "model life"?
    Sub tgr()
        
        Dim rngFound As Range
        Dim arrMerged(1 To 65000, 1 To 3) As Variant
        Dim strFirst As String
        Dim strFind As String
        Dim MergedIndex As Long
        Dim i As Long
        
        With Application.FindFormat
            .Clear
            .MergeCells = True
        End With
        
        strFind = "model life"
        
        Set rngFound = Cells.Find(strFind, Cells(Rows.Count, Columns.Count), xlValues, xlPart, SearchFormat:=True)
        If Not rngFound Is Nothing Then
            strFirst = rngFound.Address
            Do
                MergedIndex = MergedIndex + 1
                arrMerged(MergedIndex, 1) = rngFound.MergeArea.Address
                arrMerged(MergedIndex, 2) = rngFound.Column
                arrMerged(MergedIndex, 3) = rngFound.Column + rngFound.MergeArea.Columns.Count - 1
                Set rngFound = Cells.Find(strFind, rngFound, xlValues, xlPart, SearchFormat:=True)
            Loop While rngFound.Address <> strFirst
            For i = 1 To MergedIndex
                MsgBox "Merged Cell Address:" & vbTab & arrMerged(i, 1) & Chr(10) & _
                       "Start Column Number:" & vbTab & arrMerged(i, 2) & Chr(10) & _
                       "End Column Number:  " & vbTab & arrMerged(i, 3)
            Next i
        End If
        
        Application.FindFormat.Clear
        Set rngFound = Nothing
        Erase arrMerged
        
    End Sub
    Btw, the reason I like the .Find method is because it won't waste time looking at cells that are not merged.

  6. #6
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    83

    Re: VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

    nailed it thanks tigeravatar. tbh I thought yours was a little long winded but actually yours is far more efficient just took a little while to get my head around!

    thanks your a star

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA to return the column numbers of the start and finish of a merged cell? RANDOM!

    You're very welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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