Automatically merge cells when a certain value is detected

CBWiggle

New Member
Joined
Aug 24, 2016
Messages
2
I'm trying to create a code that will make a selection of cells in a row automatically merge when a value in column A appears.

If the value "Group 1" or "Group 2" appears in cell A2, the range A2:N2 should merge. I'd need to use this code in other rows of the spreadsheet too if possible, and if the value in column A is changed again, the cells should automatically return to being separate (not merged).

Is this possible?

I'm very new to VBA, so thanks for your help!
Charlotte
 

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.
Re: Automatically merge cells when a certain value is detected [ Please help ]

do they have to merge - it would be easy to color A2:N2 red if A2 = group1 or group2

and if you changed it to group 3 the red color would disappear
 
Upvote 0
Re: Automatically merge cells when a certain value is detected [ Please help ]

They don't have to merge i suppose!

With that in mind, i just tried to make a code for it, but it doesn't seem to be working :/

Could you help me figure out what i need to change in this please?

Code:
Sub Macro1()
'
' Macro1 Macro
'
Dim group As String
Set group.Value = Range("A2").Value


If group.Value = "Group 1" Or group.Value = "Group 2" Then


'    Range("A2:N2").Select
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent5
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    Range("A2").Select
End Sub

Thanks :)
Charlotte
 
Upvote 0
Re: Automatically merge cells when a certain value is detected [ Please help ]

I cannot see where in your code you are merging cells

Range("A1:E1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Selection.Merge
End Sub

this code merges A1:A5
 
Upvote 0

Forum statistics

Threads
1,216,225
Messages
6,129,603
Members
449,520
Latest member
TBFrieds

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