Excel VBA: 元素未存储在循环中
Excel VBA: Element Not Being Stored in Loop
下面的问题让我很困惑。我创建了一个函数来根据多个输入提供特定列的列索引:
- 一个数组(来自 headers 列的范围)
- 数据元素个数(Integer)
- 所选列名称的数组
- 一个数组,用于保存传递回子程序的结果。
- 计数器(全局)
显然,这里的目的是将列索引传递给其他函数和例程进行偏移和附加处理。
虽然函数本身有效,但它不适用于第二个数据元素。 K 将用适当的索引记录,但不会传递给数组。我在这里遗漏了什么吗?
Public Function getIndex(ByRef all_names As Variant, ByVal Elements As Integer, check_names() As Variant, resultindex() As Variant) As Variant
ReDim resultindex(1 To Elements)
For i = LBound(all_names) To UBound(all_names)
For j = 1 To Elements
For k = LBound(all_names, 2) To UBound(all_names, 2)
If all_names(i, k) = check_names(j) Then ' checks colName against dynamic names
resultindex(j) = k ' colIndex takes index of selected column
Debug.Print resultindex(j)
' k = UBound(all_names, 2) ' Jump to end?
End If
Next k
Next j
Next i
End Function
没有存储第二个元素是否有特殊原因?我已经用几个不同的输入尝试过这个并取得了相同的结果。非常感谢任何擅长 nested-loops 的人在这方面给我一个正确方向的点头。谢谢
编辑:看起来它正在立即工作 window。适当的索引按预期被捕获,但第二个元素没有被传递出去。
验证值是否已通过:
results(i) = getIndex(subArray(), Elements, selNames(), results())
colIndex() = results()
For i = 1 To Elements
Debug.Print colIndex(i)
Next i
Erase result
您永远不会为函数分配 return 值。您似乎还使用 resultindex
作为 ByRef
参数来保存结果。你不需要两者都做。
重构,试试
Public Function getIndex( _
ByRef all_names As Variant, _
ByVal Elements As Integer, _
check_names() As Variant) As Variant
Dim i As Long, j As Long, k As Long
Dim resultindex() As Variant
ReDim resultindex(1 To Elements)
For i = LBound(all_names) To UBound(all_names)
For j = 1 To Elements
For k = LBound(all_names, 2) To UBound(all_names, 2)
If all_names(i, k) = check_names(j) Then ' checks colName against dynamic names
resultindex(j) = k ' colIndex takes index of selected column
Debug.Print resultindex(j)
Exit For
End If
Next k
Next j, i
getIndex = resultindex
End Function
然后这样称呼它
results = getIndex(subArray(), Elements, selNames())
For i = 1 To Elements
Debug.Print results(i)
Next i
下面的问题让我很困惑。我创建了一个函数来根据多个输入提供特定列的列索引:
- 一个数组(来自 headers 列的范围)
- 数据元素个数(Integer)
- 所选列名称的数组
- 一个数组,用于保存传递回子程序的结果。
- 计数器(全局)
显然,这里的目的是将列索引传递给其他函数和例程进行偏移和附加处理。
虽然函数本身有效,但它不适用于第二个数据元素。 K 将用适当的索引记录,但不会传递给数组。我在这里遗漏了什么吗?
Public Function getIndex(ByRef all_names As Variant, ByVal Elements As Integer, check_names() As Variant, resultindex() As Variant) As Variant
ReDim resultindex(1 To Elements)
For i = LBound(all_names) To UBound(all_names)
For j = 1 To Elements
For k = LBound(all_names, 2) To UBound(all_names, 2)
If all_names(i, k) = check_names(j) Then ' checks colName against dynamic names
resultindex(j) = k ' colIndex takes index of selected column
Debug.Print resultindex(j)
' k = UBound(all_names, 2) ' Jump to end?
End If
Next k
Next j
Next i
End Function
没有存储第二个元素是否有特殊原因?我已经用几个不同的输入尝试过这个并取得了相同的结果。非常感谢任何擅长 nested-loops 的人在这方面给我一个正确方向的点头。谢谢
编辑:看起来它正在立即工作 window。适当的索引按预期被捕获,但第二个元素没有被传递出去。
验证值是否已通过:
results(i) = getIndex(subArray(), Elements, selNames(), results())
colIndex() = results()
For i = 1 To Elements
Debug.Print colIndex(i)
Next i
Erase result
您永远不会为函数分配 return 值。您似乎还使用 resultindex
作为 ByRef
参数来保存结果。你不需要两者都做。
重构,试试
Public Function getIndex( _
ByRef all_names As Variant, _
ByVal Elements As Integer, _
check_names() As Variant) As Variant
Dim i As Long, j As Long, k As Long
Dim resultindex() As Variant
ReDim resultindex(1 To Elements)
For i = LBound(all_names) To UBound(all_names)
For j = 1 To Elements
For k = LBound(all_names, 2) To UBound(all_names, 2)
If all_names(i, k) = check_names(j) Then ' checks colName against dynamic names
resultindex(j) = k ' colIndex takes index of selected column
Debug.Print resultindex(j)
Exit For
End If
Next k
Next j, i
getIndex = resultindex
End Function
然后这样称呼它
results = getIndex(subArray(), Elements, selNames())
For i = 1 To Elements
Debug.Print results(i)
Next i