insert row under a s selected cell and merge a cells

anell370

New Member
Joined
Dec 19, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
HI
Sorry I'm not familiar with VBA but I need this for my project I need to create VBA button with a code to insert a row under a random selected cell "let's say D" and then merge each of the cellS before it ",C,B,A" with it the new cell inserted under it; for example C2 with C3, B2 with B3 ....keeping the selected one unmegred

THANKS
 

Attachments

  • Sans titre.png
    Sans titre.png
    16.1 KB · Views: 10

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
The horizontal and vertical cell centering should be done before. And it does some crazy things if you are on a cell that has already been merged.

VBA Code:
Sub Button1_Click()

    Dim activeC As Long                                                 'The column of the active cell
    Dim col As Long                                                     'a counter
    Dim rng As Range                                                    'range is needed to insert below
    
    activeC = ActiveCell.Column                                         'remember the column with the active cell

    Set rng = ActiveCell.Offset(1, 0)                                   'set up the range
    rng.EntireRow.Select                                                'select that entire row
    Selection.Insert shift:=xlDown                                      'insert below
    
    col = 1                                                             'starting at column A
    While col <= ActiveSheet.UsedRange.Columns.Count                    'go through all columns until done
        If col <> activeC Then                                          'is the current column the one that was active.  If it is do not merge it
            Range(Cells(activeR, col), Cells(activeR + 1, col)).Merge   'merge the cell and the one below it
        End If
        col = col + 1                                                   'move to the next column
    Wend
    
End Sub
 
Upvote 0
The horizontal and vertical cell centering should be done before. And it does some crazy things if you are on a cell that has already been merged.

VBA Code:
Sub Button1_Click()

    Dim activeC As Long                                                 'The column of the active cell
    Dim col As Long                                                     'a counter
    Dim rng As Range                                                    'range is needed to insert below
   
    activeC = ActiveCell.Column                                         'remember the column with the active cell

    Set rng = ActiveCell.Offset(1, 0)                                   'set up the range
    rng.EntireRow.Select                                                'select that entire row
    Selection.Insert shift:=xlDown                                      'insert below
   
    col = 1                                                             'starting at column A
    While col <= ActiveSheet.UsedRange.Columns.Count                    'go through all columns until done
        If col <> activeC Then                                          'is the current column the one that was active.  If it is do not merge it
            Range(Cells(activeR, col), Cells(activeR + 1, col)).Merge   'merge the cell and the one below it
        End If
        col = col + 1                                                   'move to the next column
    Wend
   
End Sub

Hello,
Thanks a lot for replying but the code doesn't work corractly, it doesn't merge cells it just add a row below, i don't understade why.
thanks
 
Upvote 0
Try:
VBA Code:
Sub MergeCells()
    Application.ScreenUpdating = False
    Dim x As Long
    Range("A" & ActiveCell.Row + 1).Insert
    For x = 1 To ActiveCell.Column - 1
        Cells(ActiveCell.Row + 1, x).Resize(2).Merge
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,060
Messages
6,128,548
Members
449,457
Latest member
ncguzzo

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