【发布时间】:2017-06-22 14:09:29
【问题描述】:
我想创建一个机场集合。
一个机场有很多参数,但为了简单起见,我们假设一个机场类在 Airport Class Module 中是这样定义的:
'Class Airport
Public name As String ' Stores name of the airport
Public flights As Long ' Stores number of flights in that airport
然后我的模块相当大,但这是我从 Excel 文件中读取列并将值存储在机场集合中的部分,删除重复的:
Dim airports As Collection
Set airports = New Collection
'Putting airports in collection
Dim c As Range
For Each c In wsToCheck.Range("D:D")
On Error Resume Next
Dim airport As New Airport
airport.name = c.Value
airports.Add airport, c.Value
On Error GoTo 0
Next
如果我在中间做
Debug.Print airport.name
我知道名字,但是当我知道时
Debug.Print airports(1).name
什么都没有打印(但也没有错误)。
我之前使用了一个字符串集合,它正在工作。但我现在每个机场需要不止一个字符串。
我的代码有什么问题?我在使用集合吗?
【问题讨论】:
-
您在代码中的什么时候执行了 Debug.Print?当我运行这段代码时,它按预期运行。
-
我也会创建一类机场,所以你可以说
airports.AirPortExistsgetAirportRunways(strAiportCode)上面的代码将存在于构造中,或者说.populateAiports(rng as excel.range)
标签: excel vba class collections