VBA Macro for insert new line and copy formula above line

eddorena

New Member
Joined
Dec 3, 2021
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi @Fluff and other :)

Sorry for making new threads here, I have some confused with the VBA Macro for insert new line and copying the formula above.

The macro actually runs, however, not as my expecting since have a difference between Sheet1 and Sheet2, here is the macro.
VBA Code:
Private Sub CommandButton1_Click()

On Error Resume Next
Dim iRow As Long
Dim iCount As Long
Dim i As Long

iCount = InputBox _
(Prompt:="Fill number line to insert")
iRow = InputBox _
(Prompt:="Choose the start line")

For i = 1 To iCount
ThisWorkbook.Sheets("Sheet2").Rows(iRow).EntireRow.Insert
ThisWorkbook.Sheets("Sheet2").Rows(Selection.Row - 1).Copy
ThisWorkbook.Sheets("Sheet1").Rows(iRow).EntireRow.Insert
ThisWorkbook.Sheets("Sheet1").Rows(Selection.Row - 1).Copy
Next i
End Sub


When I fill the number of insert line is 5, and choose a start the insert line from line 3, on Sheet2 the line only insert 4 lines with copying the formula above (line 2), however, in Sheet1 the line insert 5 line correctly from line 3 until 7.

There is a gap 1 line between Sheet 2 and Sheet1, I wish I can get the solution
When I fill the number of insert line is 5, and choose a start the insert line from line 3, on Sheet2 the line only insert 4 lines with copying the formula above (line 2), however, in Sheet1 the line insert 5 line correctly from line 3 until 7.

There is a gap 1 line between Sheet 2 and Sheet1, I wish I can get the solution

I Attached the file Sample
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi mohadin,

shouldn' it be Step - 1?

Holger
 
Upvote 0
Are you only using this on one sheet at a time? If so, try this.
Private Sub CommandButton1_Click()

On Error Resume Next
Dim iRow As Long
Dim iCount As Long
Dim i As Long

iCount = InputBox _
(Prompt:="Fill number line to insert")
iRow = InputBox _
(Prompt:="Choose the start line")

For i = 1 To iCount
ActiveSheet.Rows(iRow).EntireRow.Insert
ActiveSheet.Rows(Selection.Row - 1).Copy
Next i
End Sub
 
Upvote 0
Hi eddorena,

please make sure that the button works if you attach a sample file (the procedure needs to be named Click_Click instead of CommandButton1_Click).

What about this:

VBA Code:
Private Sub Click_Click()

'On Error Resume Next
  Dim lngRow As Long
  Dim lngNrRows As Long
 
  lngNrRows = InputBox(Prompt:="Fill number line to insert")

''lngRow = InputBox _
''  (Prompt:="Choose the start line")
''
'/// getting the next free row on Sheet 2
  With ThisWorkbook.Sheets("Sheet2")
    lngRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
  End With

  With ThisWorkbook.Sheets("Sheet2")
    .Cells(lngRow - 1, "A").Resize(1, 8).Copy
    .Cells(lngRow, "A").Resize(lngNrRows, 8).Insert Shift:=xlDown
  End With
  With ThisWorkbook.Sheets("Sheet1")
    .Cells(lngRow - 1, "A").Resize(1, 8).Copy
    .Cells(lngRow, "A").Resize(lngNrRows, 8).Insert Shift:=xlDown
  End With
End Sub

I still wonder why you need to insert the copied row in your sample as the sheets do not hold further data below the formula rows.

Holger
 
Upvote 0
Solution
Hi eddorena,

please make sure that the button works if you attach a sample file (the procedure needs to be named Click_Click instead of CommandButton1_Click).

What about this:

VBA Code:
Private Sub Click_Click()

'On Error Resume Next
  Dim lngRow As Long
  Dim lngNrRows As Long
 
  lngNrRows = InputBox(Prompt:="Fill number line to insert")

''lngRow = InputBox _
''  (Prompt:="Choose the start line")
''
'/// getting the next free row on Sheet 2
  With ThisWorkbook.Sheets("Sheet2")
    lngRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
  End With

  With ThisWorkbook.Sheets("Sheet2")
    .Cells(lngRow - 1, "A").Resize(1, 8).Copy
    .Cells(lngRow, "A").Resize(lngNrRows, 8).Insert Shift:=xlDown
  End With
  With ThisWorkbook.Sheets("Sheet1")
    .Cells(lngRow - 1, "A").Resize(1, 8).Copy
    .Cells(lngRow, "A").Resize(lngNrRows, 8).Insert Shift:=xlDown
  End With
End Sub

I still wonder why you need to insert the copied row in your sample as the sheets do not hold further data below the formula rows.

Holger

Hi @HaHoBe

Little bit modified and it's 100 WORKS :coffee::cool:(y), Thank You for your advice it's really helpful.

VBA Code:
Private Sub Click_Click()
'On Error Resume Next
  Dim lngRow As Long
  Dim lngNrRows As Long
 
  lngNrRows = InputBox(Prompt:="Fill number line to insert")
  lngRow = InputBox(Prompt:="Fill number line to insert")

  With ThisWorkbook.Sheets("Sheet2")
    .Cells(lngRow - 1, "A").Resize(1, 8).Copy
    .Cells(lngRow, "A").Resize(lngNrRows, 8).Insert Shift:=xlDown
  End With
  With ThisWorkbook.Sheets("Sheet1")
    .Cells(lngRow - 1, "A").Resize(1, 8).Copy
    .Cells(lngRow, "A").Resize(lngNrRows, 8).Insert Shift:=xlDown
  End With
End Sub

I Apologize about button name, I forgot change.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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