Insert Row with Merged Cells

pclark

Board Regular
Joined
Feb 19, 2004
Messages
84
I have a button that inserts x number of new rows into a protected worksheet. I need some the cells in each of the rows to be merged automatically, J:L and M:O.

Thanks in advance.

Peter
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Range("J1:L1").Merge
Range("M1:O1").Merge

Or

Range("J" & x & ":L" & x).Merge

Where x is the row number
 
Upvote 0
I don't think that will work because I can not determine what the new row number is, also it does not solve the insertion of multiple rows.

Here is what I am working with:
Code:
Private Sub CommandButton1_Click()
        Dim ANS As String
        Dim x As String
        
    ActiveSheet.Unprotect password:="password"
        ANS = MsgBox("Do you want to insert new rows here?", vbYesNo, "Insert Rows")
    Select Case ANS
        Case vbYes
            x = InputBox("How many Rows do you want to insert?", "Number of Rows to Insert", "1")
        Select Case x
            Case vbOK
                Range(ActiveCell, ActiveCell.Offset(x - 1, 0)).EntireRow.Insert
                ActiveSheet.Protect password:="password"
            Case Else
                ActiveSheet.Protect password:="password"
                Exit Sub
        End Select
        Case vbNo
            ActiveSheet.Protect password:="password"
            Exit Sub
    End Select
    
End Sub

After x is answered the new row or rows need to have cells J:L and M:O merged.

Thanks again.
 
Upvote 0
Does anyone have any suggestions on how I can get the cells to automaticaly merge after any number of rows are entered?

Any suggestions would help.
 
Upvote 0
Try this. I reduced the unnecessary code, hope you don't mind.


Private Sub CommandButton1_Click()
Dim ANS As String, x As String
ANS = MsgBox("Do you want to insert new rows here?", vbYesNo, "Insert Rows")
If ANS = vbNo Then Exit Sub
x = InputBox("How many Rows do you want to insert?", "Number of Rows to Insert", "1")
If x = "" Then Exit Sub
Dim z As Long
z = ActiveCell.Row
ActiveSheet.Unprotect Password:="password"
ActiveCell.Resize(x).EntireRow.Insert
Range(Cells(z, 10), Cells(z + x - 1, 12)).MergeCells = True
Range(Cells(z, 13), Cells(z + x - 1, 15)).MergeCells = True
ActiveSheet.Protect Password:="password"
End Sub
 
Upvote 0
Try this. I reduced the unnecessary code, hope you don't mind.


Private Sub CommandButton1_Click()
Dim ANS As String, x As String
ANS = MsgBox("Do you want to insert new rows here?", vbYesNo, "Insert Rows")
If ANS = vbNo Then Exit Sub
x = InputBox("How many Rows do you want to insert?", "Number of Rows to Insert", "1")
If x = "" Then Exit Sub
Dim z As Long
z = ActiveCell.Row
ActiveSheet.Unprotect Password:="password"
ActiveCell.Resize(x).EntireRow.Insert
Range(Cells(z, 10), Cells(z + x - 1, 12)).MergeCells = True
Range(Cells(z, 13), Cells(z + x - 1, 15)).MergeCells = True
ActiveSheet.Protect Password:="password"
End Sub


Hello, thank you for this useful information. I have a question please:

Range(Cells(z, 10), Cells(z + x - 1, 12)).MergeCells = True - using this syntax when i insert 3 new rows with merge cells, it merge horizontal and vertical those cell and not creating 3 separate rows with merge cell by columns. What i must modify in the above code ?
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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