0

I have a spreadsheet for entering a new set of data on a new row each day each day, the row contains formulas and formatting. I want to be able to click a button and it adds a row under the last row with entered data and copy the formulas and formatting only, ready for new data to be entered.

Below is my code:

Sub Button1_Click()
    Dim ws As Worksheet
    Dim varUserInput As Variant

    Set ws = ThisWorkbook.Sheets("Summary")

    With ws
         varUserInput = .Range("D" & .Rows.Count).End(xlUp).Row

        .Rows(varUserInput).Insert Shift:=xlDown

        .Rows(1).Copy .Rows(varUserInput)

        .Rows(varUserInput - 1).Copy
        .Rows(varUserInput + 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    End With

    Application.CutCopyMode = False
End Sub

The issue is that it will only copy the formula from the same hidden row each time.

aLearningLady
  • 1,988
  • 4
  • 24
  • 42
J.Goddard
  • 107
  • 3
  • 11

1 Answers1

1

Is this what you are trying (UNTESTED)?

Sub Button1_Click()
    Dim ws As Worksheet
    Dim varUserInput As Variant

    varUserInput = InputBox("Enter Row Number where you want to add a row:", "What Row?")

    If varUserInput = "" Then Exit Sub

    Set ws = ThisWorkbook.Sheets("Sheet1") '<~~ Change as applicable

    With ws
        .Rows(varUserInput).Insert Shift:=xlDown

        .Rows(1).Copy .Rows(varUserInput)

        .Rows(varUserInput - 1).Copy
        .Rows(varUserInput).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    End With

    Application.CutCopyMode = False
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250