2016-11-08 34 views
0

我想在Excel中創建一個宏,但下面的代碼會導致錯誤。我不明白爲什麼通過索引獲取表導致vba宏中的錯誤

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任何幫助,將不勝感激。

回答

2

你需要使用「設置」,如果你想引用工作簿

Public Sub CreateSimpleModel() 

    Dim reportSheet As Worksheet 
    MsgBox ThisWorkbook.Sheets.Count 

    Set reportSheet = ThisWorkbook.Sheets(1) 

End Sub