I have a Sum
on specific Cells (lets say "A1:A5"
) in my Excel worksheet. Usually, whenever I insert a row manually on "A1"
, The sum is expanded, so the new Range is "A1:A6"
. Now when I insert a row using VBA at "A1"
, my sum goes from "A2:A6"
, not "A1:A6"
. How can I insert a row but basically keep the current formatting (so that the sum is automatically expanded)?
My code I use for inserting:
dest.Rows(index).Insert shift:=xlShiftDown
dest.Rows(index).PasteSpecial xlPasteValues
where dest
is the worksheet I want to expand.
Edit:
I tried to use a macro and use that code, that did sadly not work.
The generated code:
Rows("167:167").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
My new code:
dest.Rows(index).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
dest.Rows(index).PasteSpecial xlPasteValues
That did not change the outcome.
Edit2:
I am going to provide a picture to clarify what I want to do:
Whenever I manually add a line (rightclick line 152 -> insert cells
) this little table is expanded and everything is shifted down, so lets assume:
I have a SUM
in cell B,180
that goes from B:50 to B:179, now the SUM
is in cell B,181
and goes from B:50
to B:180
. Also, the formatting of the table is used for the new line (as far as the table goes, so columns A:K
are having the background/borders of the cells above.
When I use my code for that, it is just a new blank line inserted without keeping the formatting.
For me this worked:
Sub copyExpanded()
Dim rng As Range
Set rng = ActiveSheet.Range("A5")
rng.Offset(1).EntireRow.Insert
rng.EntireRow.Copy rng.Offset(1).EntireRow
End Sub
Tags: excelexcel, vba