Unmerge Cells with Macro based on value

Alisan

New Member
Joined
Jul 19, 2021
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Dear friends, I would like to ask you the following;

I need to write a macro which looks up to column C (A dropdown menu, which is selected manually), and if the column C is called "Header", I want that Macro merges the cells between E and W on that specific line.
For this condition, I have the below code which works very fine :

Sub loopandMarge()
Dim lastrow As Long, I As Long
lastrow = Range("C" & Rows.Count).End(xlUp).Row

Application.DisplayAlerts = False
For I = 18 To lastrow
If Cells(I, "C") = "Header" Then
With Range("E" & I & ":W" & I)
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Font.Bold = True
End With
End If
Next I
Application.DisplayAlerts = True
End Sub

However when in column "C" I change the value from the dropdown menu from "Header" to another value, the cells remain merged.
I would like to have a code which can also unmerge the cells, once the value in column "C" change.

Would you be able to help me ?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Using merged cells with VBA causes endless problems, since they are usually only used for display purposes, I recommend using the "format across selection" format instead which usually gives the same appearance.
so try this:
VBA Code:
Sub loopandMarge()
Dim lastrow As Long, I As Long
lastrow = Range("C" & Rows.Count).End(xlUp).Row

Application.DisplayAlerts = False
For I = 18 To lastrow
If Cells(I, "C") = "Header" Then
With Range("E" & I & ":W" & I)
.HorizontalAlignment = xlCenterAcrossSelection
.MergeCells = False
.VerticalAlignment = xlCenter
.WrapText = True
.Font.Bold = True
End With
 Else
With Range("E" & I & ":W" & I)
.HorizontalAlignment = xlLeft
.MergeCells = False
.VerticalAlignment = xlCenter
.WrapText = True
.Font.Bold = True
End With
End If

Next I
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
Using merged cells with VBA causes endless problems, since they are usually only used for display purposes, I recommend using the "format across selection" format instead which usually gives the same appearance.
so try this:
VBA Code:
Sub loopandMarge()
Dim lastrow As Long, I As Long
lastrow = Range("C" & Rows.Count).End(xlUp).Row

Application.DisplayAlerts = False
For I = 18 To lastrow
If Cells(I, "C") = "Header" Then
With Range("E" & I & ":W" & I)
.HorizontalAlignment = xlCenterAcrossSelection
.MergeCells = False
.VerticalAlignment = xlCenter
.WrapText = True
.Font.Bold = True
End With
 Else
With Range("E" & I & ":W" & I)
.HorizontalAlignment = xlLeft
.MergeCells = False
.VerticalAlignment = xlCenter
.WrapText = True
.Font.Bold = True
End With
End If

Next I
Application.DisplayAlerts = True
End Sub
Hello Offthelip,

Thank you very much for your fast reply.Maybe there is a small error in the code, is this possible ? I run the code, however the cells remain merged, when I change the value in column C.
My best regards, ALisan
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
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