我正在使用EXCEL 2007我构建了这段代码,它从EXCEL单元格中获取数据,并将一个名为MesPoints((的Array((填充为clsPoint。该代码还使用一个名为MaCurve的数组作为clsCurve。该类在内部定义了一个名为PointsXY的变量数组。我想避免将所有数据复制到类中,所以我试图传递整个数组ByRef。在模块中,我将数据填充到MesPoints中,然后尝试将MesPoint作为变量传递到类中的PointsXY中。默认情况下,这应该通过ByRef完成。
下面的代码显示了如何调用Property Let AllDots以将整个数组作为Variant clsPoint类传递。
Private MaCurve As New clsCurve
Private MesPoints() As New clsPoint
Dim MyRange As Range
Dim MySheet As Worksheet
Dim StartRow As Long, EndRow As Long, StartCol As Long
Sub FillClasses() ' This code in into a separate module Main
Dim Pcount As Long, Scount As Long, Ccount As Long, Dcount As Long
Dim i As Integer, j As Integer, k As Integer, m As Long, pr As Long, pc As Long
Dim Y As Double, X As Double
StartRow = 4: EndRow = 9: StartCol = 2
Set MySheet = Application.Worksheets("Data1")
Set MyRange = Range(MySheet.Cells(StartRow, StartCol), MySheet.Cells(EndRow, StartCol + 1)) ' Défini le range des données
Pcount = MyRange.Rows.Count: Scount = Pcount - 1: Ccount = Scount - 1: Dcount = Ccount - 1
ReDim MesPoints(Pcount)
For i = 1 To Pcount ' Population des valeurs X et Y des points
j = 1: k = 2
MesPoints(i).X = MyRange.Cells(i, j)
MesPoints(i).Y = MyRange.Cells(i, k)
Next i
Let MaCurve.AllDots = MesPoints()
’…_____________________________________________________________________________________________
' Class MaCurve
Dim PointsXY As Variant
Public Property Let AllDots(ByRef AP() As clsPoint)
Let PointsXY = AP()
DataCount = UBound(PointsXY())
Pcount = DataCount + 1
ReDim Preserve PointsXY(Pcount)
Scount = DataCount: Ccount = Scount - 1: Dcount = Ccount - 1
For i = 1 To Scount ' Population des droites
Set Segments(i).P1 = PointsXY(i) ' The code will choke here
Next i
'...
End Property
该代码将不起作用。相反,只要调用PointsXY中的一个数据进行计算处理器在没有错误、没有警告、什么都没有的情况下退出类代码,返回主代码中的焦点并继续。
我打电话给出租房好吗?错误在哪里,因为这是一种奇怪的VBA行为?
Property Let
(或Property Set
(的值参数始终按值传递,而与说明符无关。这并不是数组独有的,但您可以通过传递Variant
指针而不是显式的、类型化的数组来解决它:
Public Property Let AllDots(ByVal Values As Variant)
我丢失了链接,但这里有相关的语言规范部分:
§如果
<property-LHS-declaration>
的<value-param>
没有<parameter-mechanism>
元素,或者有一个由关键字ByRef
组成的<parameter-mechanism>
,则其含义与它有一个包含关键字ByVal
的<parameter-mechanism>
元素相同。
请参阅此处和此处的讨论(GitHub链接到Rubberduck存储库(。