簡體   English   中英

Excel VBA從用戶輸入插入行並自動填充

[英]Excel VBA to insert a row from user input and autifill down

我有以下代碼根據用戶輸入的行號在多張紙上插入一行並自動填充插入的行。

    Sub AddNewRep()

    Dim SelRow As Integer, i As Integer, j As Integer
    If Selection.Rows.Count > 1 Then Exit Sub
    SelRow = Selection.Row
    On Error GoTo nonNumeric
    j = InputBox("ENTER ROW TO INSERT NEW REP", "ADD REP")
    On Error GoTo 0
    GoTo NumericEntry

nonNumeric:
    On Error GoTo 0
    MsgBox ("Please try again with a number.")
    Exit Sub

NumericEntry:
    For i = 1 To 9
        Sheets(i).Select
        Rows(j).Insert
        Sheets(i).Select
        Rows(j - 1).Select
        Selection.AutoFill Destination:=Rows("(j - 1):(j)"), Type:=xlFillDefault
        On Error Resume Next
            Rows(j).SpecialCells(xlCellTypeConstants).ClearContents
        On Error GoTo 0
    Next i

    Sheets(1).Select

    ActiveCell.FormulaR1C1 = ""
End Sub

我在行上收到“運行時錯誤'13':類型不匹配”:

Selection.AutoFill Destination:=Rows("(j - 1):(j)"), Type:=xlFillDefault

任何人對我在這里做錯什么都有理想嗎?

提前致謝!

通過將變量用引號引起來,它們被解釋為String 您只需要在周圍加上引號:

更改

Selection.AutoFill Destination:=Rows("(j - 1):(j)"), Type:=xlFillDefault 

至:

Selection.AutoFill Destination:=Rows(j - 1 & ":" & j), Type:=xlFillDefault

編輯:我也很確定您將在這里需要一些列引用,例如:

Selection.AutoFill Destination:=Range("A" & j - 1 & ":G" & j), Type:=xlFillDefault

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM