VBA: Insert row + keep formulas

S42k20

New Member
Joined
Jul 12, 2020
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I already read some threads in this big forum but didn't find what I am looking for. I hope it is ok to ask for some help on this:

I have a table and a button to click in A1.
If I click this button, a new row shall be inserted below the current active cell including only the above used formulas in every cell of the above row. The values should not be copied.

What I had before is:

VBA Code:
ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Offset(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.EntireRow.Offset(1).SpecialCells(xlCellTypeConstants).Clear

But an error occures, when the active cell is then in one of the new created cells "Runtime Error 1004 - No cells found" , which makes sense but what is the clue here :)?

Thanks a lot best regards!
 
Hi All,

I've used your very useful VBA code below but it doesn't work when I am protecting the worksheet. Any idea on how to solve this?

With ActiveCell.EntireRow
.Copy
.Offset(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
On Error Resume Next
.Offset(1).SpecialCells(xlCellTypeConstants).Value = ""
Application.CutCopyMode = False
On Error GoTo 0
End With


I've also added the below attached macro code in my sheet to delete rows, but the same problem appears when the worksheet is protected. Could you please advise how to solve it?

Sub DeleteEntireRow()

Selection.EntireRow.Delete

End Sub
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi & welcome to MrExcel.
Please start a new thread for this question. Thanks
 
Upvote 0
How about
VBA Code:
   With ActiveCell.EntireRow
      .Copy
      .Offset(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
       On Error Resume Next
      .Offset(1).SpecialCells(xlCellTypeConstants).Value = ""
      Application.CutCopyMode = False
      On Error GoTo 0
   End With
Hi Fluff,

Sorry for opening again that topic. I've used your code that works perfectly in my excel sheet as well! :)

I just have a question. How can I modify the above code to insert copied rows above the "total bar"?

Something like?

For i = 5 To lastrow
If inarr(i, 1) <> "TOTAL:" Then ?

1652178862642.png
 
Upvote 0
This is a totally different question, so once again needs a new thread.
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
   With ActiveCell.EntireRow
      .Copy
      .Offset(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
      On Error Resume Next
      .Offset(1).SpecialCells(xlCellTypeConstants).Clear
      On Error GoTo 0
   End With
Hello,
Using your exact code above, how can you shift down 5 rows while keeping the formula?
Thank you.
 
Upvote 0
Hi Fluff, hope you are doing well and healthily.

Sorry to jump in on this thread, I think this is related to my cases.

I have VBA code below to insert and copy the formula above line, however, I feel still not working smoothly.

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

My expectation is, after click the button I fill the number of lines that need to insert then select the start line, however, the result it is not in accordance with expectation

Between Sheet1 and Sheet2 not same result, example :
I insert 5 lines, and start from line 3, in Sheet2 the result only 4 line ( 5 include the line2) and Sheet1 the result is correct.

Please your advise @Fluff, I Attached the file Sample

Thank You and Have a Nice Weekend
 
Upvote 0
This has nothing to do with the original question. So as mentioned previously please start a thread of your own.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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