简体   繁体   中英

vba to insert row at bottom of data range and copy formatting and formulas not working

I have some code which works perfectly apart from the fact that it copies the content of the above cells as well. I'd like the cells to be inserted empty with the formulas only.

I'd also like another button that removes unused rows...is this possible before i try and figure out the code for that?

在此处输入图片说明

i have tried inserting rows.clear but this cleared every row in the whole spreadsheet! I tried inserting this line and similar variations in different areas of the code and i cant get it to work..


  Dim i As Long, n As Variant
  n = InputBox("How many rows:", "INSERT ROWS")
  If n = "" Or Not IsNumeric(n) Or n < 1 Then Exit Sub
  If Int(n) < Val(n) Then Exit Sub
  i = 15
  Do While Cells(i, "B") <> ""
    i = i + 1
  Loop
  Rows(i & ":" & i + n - 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
  Rows(i - 1 & ":" & i - 1).Copy
  Rows(i & ":" & i + n - 1).PasteSpecial Paste:=xlPasteFormulas
  Rows(i & ":" & i + n - 1).PasteSpecial Paste:=xlPasteFormats
  Application.CutCopyMode = False
  End Sub

i need the button to insert a specified number of rows at the bottom of the data range that is empty with the correct formulas and formatting. It works perfectly apart from the rows have copied the content of the cell above as well. please note if the contents from B, G and H are removed this will clear the row as these cells do not have formulas in them I'd also like a button that removes empty unwanted rows from the bottom of the data range.

I have also noticed that i can't undo the inserted rows...is there a piece of code that enables the undo function?

This is used by selecting a number of cells (in the first column for example) then run the macro which copies the entire row of the first cell in that selection and pastes it in inserted rows below by the number of beforehand selected rows then removes non-formulas.

Sub add()
    With Selection
        .Cells(1).EntireRow.Copy
        .Offset(1).EntireRow.Insert shift:=xlShiftUp, CopyOrigin:=xlFormatFromLeftOrAbove
        Application.CutCopyMode = False
        On Error Resume Next
        .Offset(1).Resize(.Rows.Count).EntireRow.SpecialCells(xlConstants).ClearContents
        On Error GoTo 0
    End With
End Sub

And this removes the entire row(s) of selection.

Sub del()
    Selection.EntireRow.delete
End Sub

在此处输入图片说明

I would have done this :

  Rows(i & ":" & i + n - 1).PasteSpecial _
Paste:=xlPasteFormulas, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
 Rows(i & ":" & i + n - 1).Range("B" & LastRow + 1).Resize(, 15).PasteSpecial _
Paste:=xlPasteFormats, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False

Please note that this will copy formula if it is formula and constant if they are constant. The last step is to delete constant of your range ...

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM