逐个索引会导致vba宏出错

时间:2016-11-08 10:23:37

标签: excel-vba excel-2010 vba excel

我正在尝试在Excel中创建一个宏,但以下代码会导致错误。我不明白为什么

Public Sub CreateSimpleModel()

    Dim reportSheet As Worksheet
    MsgBox ThisWorkbook.Sheets.Count

    reportSheet = ThisWorkbook.Sheets(1) <-- Here I get the error

End Sub

出现消息框,表明工作簿中有工作表。错误object variable or with block not set任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

你需要使用&#34; Set&#34;如果你想参考工作簿

Public Sub CreateSimpleModel()

    Dim reportSheet As Worksheet
    MsgBox ThisWorkbook.Sheets.Count

    Set reportSheet = ThisWorkbook.Sheets(1)

End Sub