Merging and unmerging cells based on another cell's value

cpg84

Active Member
Joined
Jul 16, 2007
Messages
266
Platform
  1. Windows
I want to merge C2:F2 if B2 has a value in it and unmerge if it doesn't.

I found code online and have tweaked it so the merging works. However, I am having trouble with unmerging the cells when there is no value in B2.

Does anyone know how to manipulate this coding so it works? I am aware that there is an exit sub statement for when rng1 is nothing, so I know that has to change. Just having trouble with putting the right statement in the right place.

I also assume I don't need "rng2" because I am only using one range (B:B).

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Intersect(Target, Range("B:B"))
If rng1 Is Nothing Then Exit Sub
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
For Each rng2 In rng1
'If rng2.Row > 1 Then
If (rng2.Row - 2) Mod 1 = 0 Then
With rng2.Offset(0, 1).Resize(1, 4).Cells
.MergeCells = True
.WrapText = True
End With
End If
'End If
Next
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub

If I need to clarify anything, please let me know

Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
My advice is to avoid merge cells if possible, but try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng1 As Range
    Set rng1 = Intersect(Target, Range("B:B"))
    
    If rng1 Is Nothing Then Exit Sub
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    For Each rng1 In Target
        If Not IsEmpty(rng1) Then
            ' If B2 has a value, merge C2:F2
            If rng1.Row = 2 Then
                With rng1.Offset(0, 1).Resize(1, 4)
                    .MergeCells = True
                    .WrapText = True
                End With
            End If
        Else
            ' If B2 is empty, unmerge C2:F2
            If rng1.Row = 2 Then
                With rng1.Offset(0, 1).Resize(1, 4)
                    .UnMerge
                End With
            End If
        End If
    Next
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,114
Members
453,021
Latest member
Justyna P

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