arrays - 编译错误:ByRef 参数类型不匹配:如何调用数组中的值
问题描述
在循环中,我想检查值是否在数组中,如果是,则跳到下一次迭代。
不过,我对数组的低理解阻碍了我:
我正在使用下面的函数(来自:Check if a value is in an array or not with Excel VBA)来查看该值是否在数组中。
Public Function IsInArray(Vtobefound As Long, arr As Variant) As Boolean
Dim i
For i = LBound(arr) To UBound(arr)
If arr(i) = Vtobefound Then
IsInArray = True
Exit Function
End If
Next i
IsInArray = False
End Function
但我下面的子仍然不起作用:
Sub CountCellstest()
Dim i, k As Long
' Dim iArray() As Single
ReDim iArray(1 To 1) As Single
For i = 1 To 3
If IsInArray(i, iArray) Then 'ERROR HERE on the i
GoTo next_iteration
End If
ReDim aArray(1 To 1) As Single
iArray(UBound(iArray)) = 2
ReDim Preserve iArray(1 To UBound(iArray) + 1) As Single
'DO smth
MsgBox "test"
next_iteration:
Next i
End Sub
错误来自以下行:
If IsInArray(i, iArray) Then
我得到Compile error: ByRef arugment type mismatch
函数 IsInArray 需要很长,我在公式中放了很长,所以我不明白这个问题......有人能解释一下吗?
解决方案
常见的错误。您的i
变量实际上是Variant
不匹配的。您必须像这样单独键入所有变量:
Dim i As Long, k As Long
推荐阅读
- python - 绘制从 CSV 文件派生的数据框(带有特定时间)
- c++ - 如何将输入存储为字符串,直到在 C++ 中输入了一个或多个特定字符串?
- c++ - 在执行虚纯函数时调用构造函数抛出“分配抽象类类型的对象”错误
- javascript - 试图从抛出未定义的函数中获取数据
- google-cloud-platform - BigQuery 中 Hive 的显示分区的替代方案
- c# - Websockets:从NodeJS websocket服务器到带有WebSocketSharp的C#客户端的多个响应
- javascript - 我想通过使用来自 Firestore 的地图来显示我的标签“等素食主义者、意大利面……等等”?
- acumatica - Acumatica - 数据视图 LSAMProdItem_lotseropts
- firebase - 从 Firebase 读取 UserData 的问题
- python - 从系统路径anaconda git repo的子目录导入python文件