后期绑定功能的问题

Trouble with late-bound functions

您好我有以下代码:

Private Sub makeNewReports()


Dim wkSheet As Worksheet
Set wkSheet = ActiveWorkbook.Worksheets("Grades")

Dim i As Long

Dim myMap As Dictionary
Set myMap = New Dictionary


For i = 4 To 6 'wkSheet.Range("a1").End(xlToRight).Column - 1
    Dim myVals As dateValueItem
    myVals.total_value = wkSheet.Cells(2, i)
    myVals.items = wkSheet.Cells(1, i)
    myVals.student_value = wkSheet.Cells(4, i)
    myMap.Add wkSheet.Cells(3, i), myVals
Next i
End Sub

以及 dateValueItem 的以下代码

Option Explicit

Public Type dateValueItem
      total_value As Long
      items As String
      student_value As Long
End Type

当我运行上面的代码时,我遇到了问题

'Compile Error: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late bound functions'

我正在尝试将 3 个不同的值映射到一个特定的日期;日期在第三行。第 2 行和第 4 行包含数值,第 1 行包含字符串值。希望能够将所有这些组织在一起,以便我最终可以将描述和值与日期联系起来。

这可能是最快的方法。创建一个 class 并将其命名为 dateValueItem

Option Explicit

Public total_value As Long
Public items As String
Public student_value As Long

并将您的代码更改为

Option Explicit

Private Sub makeNewReports()
Dim wkSheet As Worksheet
    Set wkSheet = ActiveWorkbook.Worksheets("Grades")

    Dim i As Long

    Dim myMap As Dictionary
    Set myMap = New Dictionary

    Dim myVals As dateValueItem
    For i = 4 To 6    'wkSheet.Range("a1").End(xlToRight).Column - 1
        Set myVals = New dateValueItem
        myVals.total_value = wkSheet.Cells(2, i)
        myVals.items = wkSheet.Cells(1, i)
        myVals.student_value = wkSheet.Cells(4, i)
        myMap.Add wkSheet.Cells(3, i), myVals
    Next i
End Sub