Excel 具有For VBA循环的多维数组
正在尝试检查第一列中的值(即多维数组中的列),如果它匹配,请对另一列中与该行匹配的值进行排序 我认为我做得不对,但这是我第一次弄乱多维数组 我是否需要在每个for循环中使用UBound和LBound来告诉它要查看哪个列 除了对当前问题的回答/解决方案外,我还想了解未来使用此方法的最佳实践方法 代码: 对。和 函数允许您指定等级。这将使您的应用程序可用于。。接下来循环遍历所有数组元素Excel 具有For VBA循环的多维数组,excel,vba,loops,multidimensional-array,Excel,Vba,Loops,Multidimensional Array,正在尝试检查第一列中的值(即多维数组中的列),如果它匹配,请对另一列中与该行匹配的值进行排序 我认为我做得不对,但这是我第一次弄乱多维数组 我是否需要在每个for循环中使用UBound和LBound来告诉它要查看哪个列 除了对当前问题的回答/解决方案外,我还想了解未来使用此方法的最佳实践方法 代码: 对。和 函数允许您指定等级。这将使您的应用程序可用于。。接下来循环遍历所有数组元素 debug.print LBound(COAArray, 1) & ":" & UBound(C
debug.print LBound(COAArray, 1) & ":" & UBound(COAArray, 1)
debug.print LBound(COAArray, 2) & ":" & UBound(COAArray, 2)
If AnotherValue = "Bananas" Then
For i = LBound(COAArray, 1) To UBound(COAArray, 1)
For j = LBound(COAArray, 2) To UBound(COAArray, 2)
If COAArray(i, j) = ThisValue Then CoaAmt = COAArray(i, j)
Next j
Next i
End If
你的数组元素分配有点混乱。应该更接近,
COAArray(0, 0) = "Apples"
COAArray(1, 0) = "Oranges"
COAArray(2, 0) = "Peaches"
COAArray(3, 0) = "Pomegranates"
COAArray(0, 1) = 498
COAArray(1, 1) = 505
COAArray(2, 1) = 564
COAArray(3, 1) = 556
COAArray(0, 2) = 570
COAArray(1, 2) = 573
COAArray(2, 2) = 742
COAArray(3, 2) = 750
例如,对于上面修复的数组分配,coarray(0,0)是Apples,coarray(0,1)是498,coarray(0,2)是570。下面是498和570
Dim i As Long, j As Long
Dim COAArray(3, 2) As Variant, CoaAmt(0 To 1) As Variant
Dim ThisValue As String, AnotherValue As String
AnotherValue = "Bananas"
ThisValue = "Apples"
COAArray(0, 0) = "Apples"
COAArray(1, 0) = "Oranges"
COAArray(2, 0) = "Peaches"
COAArray(3, 0) = "Pomegranets"
COAArray(0, 1) = 498
COAArray(1, 1) = 505
COAArray(2, 1) = 564
COAArray(3, 1) = 556
COAArray(0, 2) = 570
COAArray(1, 2) = 573
COAArray(2, 2) = 742
COAArray(3, 2) = 750
If AnotherValue = "Bananas" Then
For i = LBound(COAArray, 1) To UBound(COAArray, 1)
If COAArray(i, 0) = ThisValue Then
For j = LBound(COAArray, 2) + 1 To UBound(COAArray, 2)
CoaAmt(j - 1) = COAArray(i, j)
Next j
End If
Next i
End If
MsgBox "The value of CoaAmt is " & CoaAmt(LBound(CoaAmt)) & " " & CoaAmt(UBound(CoaAmt))
我必须将您的
CoaAmt
var更改为一维变量数组,以便收集这两个数字并输出它们。我想您需要LBound
和UBound
方法返回数组的上下边界:For I=LBound(COAArray)到UBound(COAArray)
为什么需要调试。打印“等等“在两行上?我以为你在二维数组的秩上遇到了麻烦。1和2是等级位置。我把它放进去是为了证明等级的正确使用。它按这个顺序返回0:3
和0:2
。我想我在尝试对多维数组进行正确排序时遇到了更多问题。我最终得到了一个类型不匹配错误,但这是因为studio认为COAArray的值等于Apple。我想我对多维数组的想法是错误的。我用电子表格的术语来考虑它(即,在列a中查找值,并从列中查找相应的行值)。也许阵列不是正确的方向?不用担心。正如我在本例序言中所提到的,您希望循环使用COAArray(i,0)
,直到找到它。然后,您需要将COAArray(i,1)
和COAArray(i,2)
放入标准一维数组CoaAmt中。由于COAMT中的可用位置是COAMT(0)
和COAMT(1)
您必须从1开始,以获得COAARAY第二列中的正确位置,并将j-1用于COAMT中的正确位置。不,听起来是一样的。可能有助于获得具有预期结果的真实数据,以可视化情况。
Dim i As Long, j As Long
Dim COAArray(3, 2) As Variant, CoaAmt(0 To 1) As Variant
Dim ThisValue As String, AnotherValue As String
AnotherValue = "Bananas"
ThisValue = "Apples"
COAArray(0, 0) = "Apples"
COAArray(1, 0) = "Oranges"
COAArray(2, 0) = "Peaches"
COAArray(3, 0) = "Pomegranets"
COAArray(0, 1) = 498
COAArray(1, 1) = 505
COAArray(2, 1) = 564
COAArray(3, 1) = 556
COAArray(0, 2) = 570
COAArray(1, 2) = 573
COAArray(2, 2) = 742
COAArray(3, 2) = 750
If AnotherValue = "Bananas" Then
For i = LBound(COAArray, 1) To UBound(COAArray, 1)
If COAArray(i, 0) = ThisValue Then
For j = LBound(COAArray, 2) + 1 To UBound(COAArray, 2)
CoaAmt(j - 1) = COAArray(i, j)
Next j
End If
Next i
End If
MsgBox "The value of CoaAmt is " & CoaAmt(LBound(CoaAmt)) & " " & CoaAmt(UBound(CoaAmt))