Macros to insert a new row

lbradbury

New Member
Joined
May 14, 2020
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a sheet that has rows of data, in which they are not in a table. There are formulas within the rows, and currently we manually insert a row so the formulas copy to the newly inserted row.

I would like to automate this process. The issue I'm posed with is that the row numbers are variable and change as new records are entered. Below is a snipit of the sheet. The intention is to insert a row about the green line, but as the new data is inserted the row number will change.


1602678274766.png
 

Attachments

  • 1602678109990.png
    1602678109990.png
    11.9 KB · Views: 14

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
This will check the cell above the one you click in, as long as that cell is not blank, it will auto populate the 8 cells from A to H with the formula from the cell above it

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim MyRow As Integer
Dim MyCol As Integer
Dim i  As Integer

MyRow = Target.Row
MyCol = Target.Column
If MyRow <> 1 Then
    If Cells(MyRow - 1, MyCol) <> "" Then

        
        For i = 1 To 8
        
           Cells(MyRow - 1, i).Copy Cells(MyRow, i)
        Next

    
    End If
End If
End Sub
 
Upvote 0
This will check the cell above the one you click in, as long as that cell is not blank, it will auto populate the 8 cells from A to H with the formula from the cell above it

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim MyRow As Integer
Dim MyCol As Integer
Dim i  As Integer

MyRow = Target.Row
MyCol = Target.Column
If MyRow <> 1 Then
    If Cells(MyRow - 1, MyCol) <> "" Then

       
        For i = 1 To 8
       
           Cells(MyRow - 1, i).Copy Cells(MyRow, i)
        Next

   
    End If
End If
End Sub



Thank you for this! I should have mentioned that the data is being inputted into a form within another worksheet, when the macros button is pressed the data it so be inserted in the row of another worksheet. Does that make sense?
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,834
Members
449,051
Latest member
excelquestion515

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