Merge & Unmerge Cells

decent_boy

Board Regular
Joined
Dec 5, 2014
Messages
130
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I want excel vba code on worksheet event my question is that i have data from g8:g28 and if g8 = 2 then unmerge a8:a10: and merge a8:a9 and if g8=3 then then unmerge a8:a10: and merge a8:a10
if g9 = 2 then unmerge a8:a10: and merge a9:a10 and if g9=3 then then unmerge a8:a10 and merge a9:a11 Similar this is with all data from g8:g28

I have developed some below code but I do not understand that how should these code can be concise and apply on cell g8 to g28

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.DisplayAlerts = False

Select Case [G8].Value
    Case Is = 1
        Range("A8:A10,F8:F10,G8:G10").UnMerge
    Case Is = 2
        Range("A8:A10,F8:F10,G8:G10").UnMerge
        Range("A8:A9,F8:F9,G8:G9").Merge
    Case Is = 3
        Range("A8:A10,F8:F10,G8:G10").UnMerge
        Range("A8:A10,F8:F10,G8:G10").Merge
  End Select

On Error Resume Next


Application.DisplayAlerts = True
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I am not following your question. The code you showed here will run anytime that there is a change to any cell on the worksheet. When it runs it takes action based on the value in cell G8 (no matter what cell was changed).

Please describe what you want to happen instead.

Also just about everyone here will recommend against using merged cells, especially when you are using VBA. They usually cause more problems than they solve.
 
Upvote 0
I am not following your question. The code you showed here will run anytime that there is a change to any cell on the worksheet. When it runs it takes action based on the value in cell G8 (no matter what cell was changed).

Please describe what you want to happen instead.

Also just about everyone here will recommend against using merged cells, especially when you are using VBA. They usually cause more problems than they solve.
 
Upvote 0
Actual I have range from G8 to G28 and i am talking about mentioned range, let me explain you that you saw code based on G8 and if G9 changes then

VBA Code:
Select Case [G9].Value
    Case Is = 1
        Range("A9:A11",F9:F11,G9:G11").UnMerge
    Case Is = 2
        Range("A9:A11,F9:F11,G9:G11").UnMerge
        Range("A9:A10,F9:F10,G9:G10").Merge
    Case Is = 3
        Range("A9:A11,F9:F11,G9:G11").UnMerge
        Range("A9:A11,F9:F11,G9:G11").Merge
  End Select

Likewise if it is G10 to G28 if any change
 
Upvote 0
Upvote 0
It is possible but writing the new code will take more time that I am able to offer right now. Sorry.
 
Upvote 0

Forum statistics

Threads
1,215,566
Messages
6,125,593
Members
449,237
Latest member
Chase S

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