VBA Code To Perform a Merge & Center

VinceF

Board Regular
Joined
Sep 22, 2007
Messages
172
Office Version
  1. 2016
Platform
  1. Windows
Greetings,
Thank you for any assistance.
Cell E3 contains a drop down list with #'s 2, 3 & 4 available to select.
If #2 is selected it would merge & center cells N11 & N12
If #3 is selected it would merge & center cells N11, N12 & N13
If #4 is selected it would merge & center cells N11, N12 &N14

Win10
Office 2016

VinceF
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If #4 is selected it would merge & center cells N11, N12 &N14
I am assuming that you actually meant: N11, N12, N13, & N14, because I don't understand how you would merge a disjointed range (not possible!).

Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this VBA code in the VB Editor that opens up:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range, cell As Range
    
'   Trigger if cell E3 is updated
    If Not (Intersect(Target, Range("E3")) Is Nothing) Then
'       First unmerge all cells in N11:N14
        Range("N11:N14").UnMerge
'       Merge appropriate cells based on selection
        Select Case Range("E3").Value
            Case 2
                Range("N11:N12").Merge
            Case 3
                Range("N11:N13").Merge
            Case 4
                Range("N11:N14").Merge
        End Select
    End If

End Sub
This should automatically do what you want, as you manually change the value in cell E3.
 
Upvote 0
I am assuming that you actually meant: N11, N12, N13, & N14, because I don't understand how you would merge a disjointed range (not possible!).

Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this VBA code in the VB Editor that opens up:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range, cell As Range
   
'   Trigger if cell E3 is updated
    If Not (Intersect(Target, Range("E3")) Is Nothing) Then
'       First unmerge all cells in N11:N14
        Range("N11:N14").UnMerge
'       Merge appropriate cells based on selection
        Select Case Range("E3").Value
            Case 2
                Range("N11:N12").Merge
            Case 3
                Range("N11:N13").Merge
            Case 4
                Range("N11:N14").Merge
        End Select
    End If

End Sub
This should automatically do what you want, as you manually change the value in cell E3.
Joe,

Thank you for taking the time to offer assistance...your code works fine and as hoped with the exception that it's not unmerging the cells. When there's not a number selected in cell E3 the word SELECT is in there, I suspect that has something to do with it?

VinceF
 
Upvote 0
Joe,

Thank you for taking the time to offer assistance...your code works fine and as hoped with the exception that it's not unmerging the cells. When there's not a number selected in cell E3 the word SELECT is in there, I suspect that has something to do with it?

VinceF
OK, you hadn't mentioned that requirement in your original post.

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

    Dim rng As Range, cell As Range
    
'   Trigger if cell E3 is updated
    If Not (Intersect(Target, Range("E3")) Is Nothing) Then
'       First unmerge all cells in N11:N14
        Range("N11:N14").UnMerge
'       Merge appropriate cells based on selection
        Select Case Range("E3").Value
            Case 2
                Range("N11:N12").Merge
            Case 3
                Range("N11:N13").Merge
            Case 4
                Range("N11:N14").Merge
            Case Else
                Range("N11:N14").UnMerge
        End Select
    End If

End Sub
 
Upvote 0
Solution
Joe4,

Works great...thanks again for taking the time to assist, very much appreciated...!

VinceF
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,363
Messages
6,124,505
Members
449,166
Latest member
hokjock

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