VBA Script to merge cells based on cell value

sam1121

New Member
Joined
Oct 26, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I'm having difficulty finding a way to merge cells based on their values. Here’s what I need:

I have two dependent dropdown menus in columns D and E. When the value "DD9900" is selected in cell E12, I would like a script that merges E12 with E13 and also merges D12 with D13.

To clarify, when the value is DD9900 in E12, the following merges should occur:

-Cell E12 merges with E13

-Cell D12 merges with D13



Example:
Screenshot 2024-10-26 133110.png



ChatGPT helped me create a solution for a single cell (E12), but I need a code that applies the same logic to the entire range from E12 to E52.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ' Check if the changed cell is E12
    If Not Intersect(Target, Me.Range("E12")) Is Nothing Then
        Application.EnableEvents = False
        ' Clear any previous merges in D12:D13 and E12:E13
        If Me.Range("D12:D13").MergeCells Then Me.Range("D12:D13").Unmerge
        If Me.Range("E12:E13").MergeCells Then Me.Range("E12:E13").Unmerge

        ' Check the value in E12
        If Me.Range("E12").Value = "DD9900" Then
            ' Merge D12:D13 and E12:E13
            Me.Range("D12:D13").Merge
            Me.Range("E12:E13").Merge
        End If
        Application.EnableEvents = True
    End If
End Sub


Can anyone assist me with this?

Thank you!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the Board!

Try this code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r As Long

    ' Exit if more than one cell updated
    If Target.CountLarge > 1 Then Exit Sub
    
    ' Check if the changed cell in E12:E52
    If Not Intersect(Target, Range("E12:E52")) Is Nothing Then
        ' Get row number of change
        r = Target.Row
        Application.EnableEvents = False
        ' Clear any previous merges in D12:D13 and E12:E13
        If Range("D" & r & ":D" & r + 1).MergeCells Then Range("D" & r & ":D" & r + 1).UnMerge
        If Range("E" & r & ":E" & r + 1).MergeCells Then Range("E" & r & ":E" & r + 1).UnMerge

        ' Check the value in column E
        If Range("E" & r).Value = "DD9900" Then
            ' Merge cells
            Range("D" & r & ":D" & r + 1).Merge
            Range("E" & r & ":E" & r + 1).Merge
        End If
        Application.EnableEvents = True
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top