在变量上使用set时发生对象错误

时间:2019-01-08 10:19:13

标签: excel vba

无论我尝试什么,都会始终显示对象错误。我在VBA方面经验不足。

它似乎是在给“ lastrow”变量赋值时。

Const sFILE_PATH    As String = "C:\Users\Downloads\"
Const sEXTENSION    As String = ".xlsm"

Dim lastrow As Long
Dim lastrow_Offset As Long: lastrow_Offset = ThisWorkbook.Sheets("Test").Cells(Rows.Count, "h").End(xlUp).Row

Dim wbk As Workbook
Dim sFileName As String

sFileName = "2018-12"
Set wbk = Workbooks.Open(sFILE_PATH & sFileName & sEXTENSION)

Set lastrow = wbk.Sheets("Acc").Cells(Rows.Count, "C").End(xlUp).Row

1 个答案:

答案 0 :(得分:1)

您已经定义了Dim lastrow As Long,因此它不是必须为Set的对象。这是一个需要分配的变量

lastrow = wbk.Sheets("Acc").Cells(Rows.Count, "C").End(xlUp).Row

我尚未针对您的隐式工作簿结构对此进行过测试,但假设wbk.Sheets("Acc").Cells(Rows.Count, "C").End(xlUp).Row的评估结果为Long