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!
Bookmarks