1
我遇到了類對象未被識別的問題(錯誤91,對象引用未設置)。下面的代碼:類對象未識別
---Main (Standard Module)
Option Explicit
Public wbCode As Workbook
Public Sub MainSub()
Dim str As String
Dim tables As New CTables
Call SetExcelObjects
str = tables.shExclusions.Cells(20, 1) ---this produces error 91
End Sub
Public Sub SetExcelObjects()
Dim tables As New CTables
Dim str As String
Set wbCode = ThisWorkbook
Set tables.shExclusions = wbCode.Worksheets("Exclusions")
str = tables.shExclusions.Cells(20, 1) ---this line executes okay
End Sub
---CTables (Class Module)
Option Explicit
Public shExclusions As Worksheet
謝謝,熊黛林! – TSB
+1,雖然因爲我很難說出你的建議有什麼變化,我正在爲未來的讀者解釋。問題在於對象「表」沒有在Main範圍內設置。通過將對象引用傳遞給「SetExcelObjects」,「SetExcelObjects」可以定義「表」以使其可用。另一種解決方案是將表格作爲整個模塊的私有變量。 –