【发布时间】:2011-03-23 03:02:27
【问题描述】:
我仍在学习 VBA,但我无法弄清楚我在使用 Collections 对象时遇到了这么多问题。
我有一个添加自定义对象的函数(我创建了一个非常简单的类来存储一些数据),它执行典型的“读取数据、创建对象表示、将其粘贴到集合中”之类的事情。
如果我尝试向 bag.add 调用添加“密钥”,我会收到“编译错误。预期:=”消息。
如果我不这样做,那么当我运行程序时,它会显示“编译错误。参数不是可选的”并突出显示“getRevColumns = bag”行。
我这辈子都想不通这是怎么回事!我怀疑我初始化包的方式有问题?! PS:columnMap 是我自定义类的名字。
Function getRevColumns() As Collection
Dim rng As Range
Dim i As Integer
Dim bag As Collection
Dim opManCol As Integer, siebelCol As Integer
Dim opManColName As String, siebelColName As String
Dim itm As columnMap
Set bag = New Collection
Set rng = shSiebelMap.UsedRange.Columns(5)
i = 1
For i = 1 To rng.Rows.count
If StrComp(UCase(rng.Cells(i).value), "Y") = 0 Then
opManCol = rng.Rows(i).OffSet(0, -2).value
opManColName = rng.Rows(i).OffSet(0, -4)
siebelCol = rng.Rows(i).OffSet(0, -1).value
siebelColName = rng.Rows(i).OffSet(0, -3)
Set itm = New columnMap
itm.opManColName = opManColName
itm.opManColNumber = opManCol
itm.siebelColName = siebelColName
itm.siebelColNumber = siebelCol
'WHY DOESN'T IT WORK!''
bag.Add (itm)
'MsgBox "opMan Col: " & opManColName & " : " & opManCol & ". Siebel Col: " & siebelColName & " : " & siebelCol'
End If
Next i
getRevColumns = bag
End Function
【问题讨论】:
-
请问您是否也可以发布“columnMap”类?
标签: vba collections vb6 excel ms-office