-1
我有一个工作表,它在XY平面上定义了一个范围,其单元格在其范围内填充了颜色。VBA - 将2个值存储在二维数组的相同索引中
我必须跨范围内移动,并且每个填充的单元的地址存储在数组中(x,Y)
,然后分配一个独特的“速度矢量”,以每个细胞(dY, dX)
的,所以在通过整个范围要循环的结束,每个单元格有4个唯一值:x, Y, dx, dY
。
这是我的工作代码:
Dim Molecules() As Variant
ReDim Molecules(1 To lWidth, 1 To lHeight) 'lWidth and lHeight indicate a dynamic range
Dim Vector() As Variant
ReDim Vector(1 To lMolecules, 1 To lMolecules) 'lMolecules is the number of filled cells within limits
For x = LBound(Molecules) To UBound(Molecules) 'columns (x)
For Y = LBound(Molecules, 2) To UBound(Molecules, 2) 'rows (y)
If Cells(Y, x).Interior.ColorIndex <> xlNone Then
'store each filled cell address in an array
Molecules(Y, x) = Cells(Y, x).Address
MsgBox "the address of original cells is = " & Molecules(Y, x)
Randomize
'between (-5) and (5) which are the H and L values
dX = Int((H - L + 1) * Rnd() + L) 'speed vector x
dY = Int((H - L + 1) * Rnd() + L) 'speed vector y
'store the dX and dY values in an array
Vector(Y, x) = (dY, dX)
MsgBox "the speed vector is = " & Vector(Y, x)
我能地址存储Cells(Y,x)
,在这里:
Molecules(Y, x) = Cells(Y, x).Address
但是,我不能让存储值为(dY, dx)
无论使用哪种方法:Offset // Address // Cells
该部分:
'store the dX and dY values in an array
Vector(Y, x) = (dY, dX)
有没有一种实际的方法来做到这一点,还是我需要将随机值存储在工作表中,并将这些工作表单元格值分配回/插入到数组中?
我很感谢您的帮助。
它们是否对应? array分子是高度和宽度,其中Vector是填充单元的数量,可以是分子(100,100)和矢量(1,1) –
您可以使用属性为dX和dY的简单“Type”,并将其存储在您的2D阵列。或者只是做'Vector(Y,x)= Array(dX,dY)' –
@Tim Williams:谢谢,它现在的确将值存储在数组中。然而,我无法得到Vector(Y,x)索引到Msgbox的值 - 是否有任何方法来定义Msgbox表达式,以便我可以实际见证插入? – Toffes