0

这是发布与错误 91 相关的#12772920的延续。好的,情况就是这样。(1) 如果这一行被注释掉,那么 (2) 会产生错误 91。如果没有被注释掉,代码运行良好。(3) 这应该将公共 obj shCompanies 指向“公司”表,不是吗?我很困惑为什么这条线似乎没有像 (1) 那样设置 shCompanies 对象。

Option Explicit
Public wbCode As Workbook

Public Sub Main()
   Dim tables As New CTables
   Set wbCode = ThisWorkbook
   Call SetExcelObjects
   (2)Call tables.Indexing
End Sub

Public Sub SetExcelObjects()
   Dim tables As New CTables
   (3)Set tables.shCompanies = wbCode.Worksheets("Companies")   
End Sub


---Class Module CTables
Option Explicit
Public shCompanies As Worksheet

Public Sub Indexing()
   Dim rng As Long
   Dim tables As New CTables
   (1)Set tables.shCompanies = wbCode.Worksheets("Companies")
   rng = tables.shCompanies.UsedRange.Rows.Count
End Sub
4

1 回答 1

0

您需要声明tablesCTables. 然后您可以从不同的模块对其进行操作。重新声明它,因为你一直在做“擦干净”并导致你看到的错误。您当然不想在CTables.

请注意,最佳做法是将实例化表与其声明分开,因此Set tables = New CTables.Main

下面为我​​编译和运行。我认为它符合您的期望:

Public wbCode As Workbook
Public tables As New CTables

Public Sub Main()
Set tables = New CTables
Set wbCode = ThisWorkbook
Call SetExcelObjects
Call tables.Indexing 
End Sub

Public Sub SetExcelObjects()
Set tables.shCompanies = wbCode.Worksheets("Companies")
End Sub

    'Class CTables
Public shCompanies As Worksheet

Public Sub Indexing()
   Dim rng As Long
   rng = shCompanies.UsedRange.Rows.Count
End Sub
于 2012-10-08T14:01:02.787 回答