我在XY平面上有一个定义范围的工作表,其中的单元格在其限制范围内填充了颜色。
我必须跨越该范围并将每个填充的单元格地址存储在数组(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)
是否有实际的方法,或者我是否需要将随机值存储在工作表中并将这些工作表单元格值分配/插入数组?
感谢您的帮助。
答案 0 :(得分:1)
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) = Array(dY, dX) '<<< store as array
MsgBox "The speed vector is = " & Vector(Y, x)(0) & _
":" & Vector(Y, x)(1)