我遇到了无法识别类对象的问题(错误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
答案 0 :(得分: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