我正在尝试在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
任何帮助都将不胜感激。
答案 0 :(得分:2)
你需要使用&#34; Set&#34;如果你想参考工作簿
Public Sub CreateSimpleModel()
Dim reportSheet As Worksheet
MsgBox ThisWorkbook.Sheets.Count
Set reportSheet = ThisWorkbook.Sheets(1)
End Sub