Results 1 to 1 of 1

Merge and unmerge cells based on cell value.

Threaded View

  1. #1
    Registered User
    Join Date
    06-07-2012
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Red face Merge and unmerge cells based on cell value.

    Hi all,

    I'm looking for a macro to merge and unmerge specific cells in a column based on their matching their value.

    Essentially what I need the macro to do is:
    1. Unmerge and erase values in cells K11 to K50.
    2. Paste a formula from cell K10 down to K11 until K50 (its a vlookup formula which will copy down appropriately)
    3. Based on the values from the formula in cells K11 to K50, merge surrounding cells that have the same value. This formula does this step -> I've posted it just for reference.
    Option Explicit 
    Sub MergeSame() 
    Dim r As Range, c As Range 
    Dim i As Long, j As Long 
    Set r = Range("k11", Cells(Rows.Count, "K").End(xlUp)) 
    
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 
    
    For i = 1 To r.Count 
    Set c = r(i) 
    j = 0 
    Do Until c <> c.Offset(rowoffset:=1) 
    Set c = c(2) 
    j = j + 1 
    Loop 
    With Range(r(i), c) 
    .Merge 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlCenter 
    End With 
    i = i + j 
    Next i 
    
    Application.DisplayAlerts = True 
    Application.ScreenUpdating = True 
    
    End Sub
    Therefore each time this macro is called, it would erase & unmerge the current data in K11:K50. Copy the formula in K10 down to K50 and based on the formula's value, merge surrounding cells.

    Just a couple extra things:
    Could this simultaneously do this over three sections? ie. K10,K11:K50, U10,U11:U50, AE10,AE11:AE50
    I'm not concerned with potential slowdown due to macro calculation.
    Could the text be left aligned.
    Could this macro be 'called' in the following formula:
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
      Application.EnableEvents = 0
        If Not Intersect(Target, Range("i8,s8,ac8")) Is Nothing Then
            Call Macro1
        ElseIf Not Intersect(Target, Cells(7, 9)) Is Nothing Then
           Cells(8, 9).ClearContents
        ElseIf Not Intersect(Target, Cells(7, 19)) Is Nothing Then
           Cells(8, 19).ClearContents
        ElseIf Not Intersect(Target, Cells(7, 29)) Is Nothing Then
           Cells(8, 29).ClearContents
        End If
       Application.EnableEvents = 1
    End Sub
    Thanks for reading!
    Last edited by Jamison; 06-21-2012 at 01:27 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