我正在尝试在VBA中创建一个函数来返回一个数组,这样我就可以将该数组分配给一个组合框。
我收到了一个非常有用的Type Mismatch错误。
Public Function ReturnActivePlayerList() As Variant
Dim iPlayerCount As Integer
iPlayerCount = ReturnPlayerCount()
ReDim arrPlayerList(0 To iPlayerCount) As Long
Dim iCounter As Integer
iCounter = 0
Sheets("MyPlayerDB").Range("A2").Activate
Do Until ActiveCell.Value = ""
arrPlayerList(iCounter) = ActiveCell.Offset(0, 2).Value
ActiveCell.Offset(1, 0).Select
iCounter = iCounter + 1
Loop
ReturnActivePlayerList = arrPlayerList
End Function
答案 0 :(得分:0)
我试过你Code,问题可能是C列的数据类型与Type Long不匹配。
如果此列中有浮点数,请选择Double。
ReDim arrPlayerList(0 To iPlayerCount) As Double
如果您在此列中有时间,请选择日期。
ReDim arrPlayerList(0 To iPlayerCount) As Date
希望这可以帮到你。
答案 1 :(得分:0)
尝试使用此代码清除且易于理解:
Public Function ReturnActivePlayerList() As Variant
Dim iPlayerCount, counter, row As Integer
iPlayerCount = ReturnPlayerCount()
'Define as Variant type
ReDim arrPlayerList(0 To iPlayerCount) As Variant
counter = 0
'Set start row
row = 2
With Sheets("MyPlayerDB")
'Loop until cell value is equal with ""
Do While .Range("A" & row) <> ""
'Set value from C column
arrPlayerList(counter) = .Range("C" & row)
'Increase counter
counter = counter + 1
'Increase row
row = row + 1
Loop
End With
ReturnActivePlayerList = arrPlayerList
End Function