将值填充到另一个工作簿到最后一行时的运行时错误

时间:2017-04-09 17:44:29

标签: excel excel-vba vba

我正在运行代码将名称从一个工作簿粘贴到另一个工作簿并自动填充,但在运行代码时我得到运行时错误9,这是示例代码

Sub test()

Dim WB As Workbook
Dim lstRow As long

workbook.open("path")
Set WB = activeworkbook

lstRow = Range("M2").End(xlDown).Row
ThisWorkbook.Activate
Worksheets("Sheet1").Range("G3").Copy
WB3.Sheets(("I2:I") & lstRow).PasteSpecial xlpastevalue

End Sub

1 个答案:

答案 0 :(得分:0)

OPTION EXPLICIT

Sub test()    
    Dim WB As Workbook

    Set WB = workbook.open("path")
    with WB.WorkSheets("Sheet1")
        With .Range(.Cells(2, "M"), .Cells(.rows.count, "M").End(xlup))
            .Offset(1, -6).Resize(.Rows.Count -1, 1) = ThisWorkbook.Worksheets("Sheet1").Range("G3").Value
        End With
    End With

End Sub