从宏上方的行复制公式和格式

时间:2015-09-07 10:56:21

标签: excel vba excel-vba

我有一个电子表格,用于每天每天在新行上输入一组新数据,该行包含公式和格式。我希望能够单击一个按钮,它会在输入数据的最后一行下添加一行,并仅复制公式和格式,准备好输入新数据。

以下是我的代码:

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

问题是它每次只会从同一个隐藏行复制公式。

1 个答案:

答案 0 :(得分:1)

这是你正在尝试的(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