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
4

1 回答 1

1

尝试这个:

---Main (Standard Module) 
Option Explicit 
Public wbCode As Workbook 

Public Sub MainSub() 
Dim str As String 
Dim tables As New CTables 
Call SetExcelObjects(tables)
str = tables.shExclusions.Cells(20, 1) ---this produces error 91 
End Sub 

Public Sub SetExcelObjects(tables as 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 
于 2012-10-07T02:47:17.830 回答