从集合VBA访问数据

时间:2013-07-06 12:06:39

标签: oop vba

我正在尝试使用.item()访问集合中的数据。我要做的是收集集合函数fncPopCcyLst中的数据并通过.item(1)中的cbSortCcy访问它以获取行号。这是一个测试,看看我是否可以在我的集合中存储多个数据并通过.item()访问它们。但是,我得到一个VBA运行时错误'5'。有人会指导我,我做错了吗?谢谢。

以下是我的代码。

课程模块:clsSngGenUtl

Private prpSngStrVal As String
Private prpSngRowNum As Long
Private prpSngClmNum As Long

'++ Define properties
'== String row number
Public Property Get SngStrVal() As String
    SngStrVal = prpSngStrVal  
End Property
Public Property Let SngStrVal(ByRef varStrVal As String)
    prpSngStrVal = varStrVal
End Property

'++ Define properties
'== Scalar row number
Public Property Get SngRowNum() As Long
    SngRowNum = prpSngRowNum
End Property
Public Property Let SngRowNum(ByVal varRowNum As Long)
    prpSngRowNum = varRowNum
End Property

'++ Define properties
'== Single column number
Public Property Get SngClmNum() As Long
    SngClmNum = prpSngClmNum
End Property
Public Property Let SngClmNum(ByRef varClmNum As Long)
    prpSngClmNum = varClmNum 
End Property

'++ Define functions
'== function get row number
Public Function fncGetRowNum(ByRef varWbName As Workbook, ByVal varWsName As String, ByRef   varSttClm As Long) As Long

    On Error GoTo Exception

    prpSngRowNum = 0
    prpSngRowNum = varWbName.Sheets(Trim(varWsName)).Cells(Rows.Count, varSttClm).End(xlUp).Row

    fncGetRowNum = prpSngRowNum

ExitHere:
    Exit Function

Exception:
    Resume ExitHere

End Function

'== function get column number
Public Function fncGetClmNum(ByRef varWbName As Workbook, ByVal varWsName As String, ByRef varSttRow As Long) As Long

    On Error GoTo Exception

    prpSngClmNum = 0
    prpSngClmNum = varWbName.Sheets(Trim(varWsName)).Cells(varSttRow, Columns.Count).End(xlToLeft).Column

    fncGetClmNum = prpSngClmNum

ExitHere:
    Exit Function

Exception:
    Resume ExitHere

End Function`

以下是我的收藏类:clsColCcySrt

'++ Declare variables
   Private prpColCcySrt As Collection

   '++ Define properties
   Public Property Get ColCcySrt() As Collection
       Set ColCcySrt = prpColCcySrt   
   End Property
   Public Property Set ColCcySrt(varColCcy As Collection)
       Set prpColCcySrt = varColCcy
   End Property

    Public Function fncGetCcyRow(ByRef varStrVal As String) As Long

        On Error GoTo Exception

        Dim clsSngGen As clsSngGenUtl
        Dim varRowNum As Long

        varRowNum = 0
        For Each clsSngGen In Me.ColCcySrt
           varRowNum = clsSngGen.SngRowNum()
        Next clsSngGen

        '== Return value
        fncGetCcyRow = varRowNum

    ExitHere:
       Exit Function

    Exception:
        If fncGetCcyRow = 0 Then
            MsgBox "Exception: Value is <" & fncGetCcyRow & ">."
        End If
        Resume ExitHere

    End Function
`

“常规”模块填充数组:fncPopFxLst

`

    Public Function fncPopCcyLst(ByRef varWbName As String, ByRef varWsName As String, ByRef varCcyTyp As String) As Collection

       Dim clnColCcy As Collection
       Dim clsArrGen As clsArrGenUtl
       Dim clsSngGen As clsSngGenUtl
       Dim varWbName As Workbook

       Set clnColCcy = New Collection

       '== Start collecting items
       Set clsSngGen = New clsSngGenUtl
       Set varWbName = ThisWorkbook
       clsSngGen.SngStrVal = "Reuters"
       clsSngGen.SngRowNum = clsSngGen.fncGetRowNum(varWbName, varWsName, 1)
       clnColCcy.Add clsSngGen

       Set fncPopCcyLst = clnColCcy

    End Function
`

最后,子程序`

    Private Sub cbSortCcy()

        Dim clsColCcy As clsColCcySrt

        Dim varDirPth As String
        Dim varCcySrc As String
        Dim varWsStrg As String
        Dim varWbStrg As String

        varDirPth = tbDirectoryName & "\" & tbFileName
        varCcySrc = "Currency"
        varWsStrg = "List"
        varWbStrg = varDirPth

        Set clsColCcy = New clsColCcySrt
        Set clsColCcy.ColCcySrt = fncPopCcyLst(varWbStrg, varWsStrg, varCcySrc)

        'Debug.Princ clsColCcy.fncGetCcyRow("Reuters")
        Debug.Print clsColCcy.ColCcySrt.Item(1)

    End Sub
`

1 个答案:

答案 0 :(得分:0)

VBA运行时错误“5”是:

  

“无效的过程调用或参数”

在您的班级clsColCcySrt中,您有一行:

varRowNum = clsSngGen.SngRowNum()
如果SngRowNumfunction而不是property,那么

是正确的。删除括号()以调用property