VBAの配列を使用して10x10の定義された範囲をループし、その中の5個の塗りつぶされたセルをそれぞれ見つけ、列、行、列10×5アレイを使用して、これらのセルのオフセット、行オフセット、およびカラーインデックスを取得します。VBA - セルを塗りつぶすために2D配列の値を使用するとエラーが発生する
塗りつぶされたセルが見つかると、適切なオフセットを持つ列/行を使用して新しい位置を取得し、見つかった元のセルを消去し、新しいセルを埋めます。アプリケーション定義/オブジェクト定義のエラー - 私はそれを埋めるために新しいセルの配列値を使用しようとしたとき
ただし、Iは1004エラーを得ます。
これは私が使用しているコードです:
Sub FindCells()
Dim i As integer, j As Integer
Dim c As Range
Dim NewX As Integer, NewY As Integer
'this is the array I'm using
Dim Molecules() As Variant
ReDim Molecules(1 To 5, 1 To 5) '1 to 5 - number of filled cells
'1 to 5 - positions, offsets and color index
For i = 1 To UBound(Molecules)
For j = 1 To UBound(Molecules, 2)
For Each c In Worksheets("Main Screen")
If c.Interior.ColorIndex <> xlNone Then
Randomize
dX = Int((H - L + 1) * Rnd() + L) 'speed vector x
dY = Int((H - L + 1) * Rnd() + L) 'speed vector y
Molecules(i, 1) = c.Column
Molecules(i, 2) = c.Row
Molecules(i, 3) = dX
Molecules(i, 4) = dY
Molecules(i, 5) = c.Interior.ColorIndex
'new position of the cells
NewX = Molecules(i, 1) + Molecules(i, 3)
NewY = Molecules(i, 2) + Molecules(i, 4)
End If
Next c
'the array gets the new position values
Molecules(i, 1) = NewX
Molecules(i, 2) = NewY
'now color the new found cells
Cells(Molecules(i, 2), Molecules(i, 1)).Interior.ColorIndex = Molecules(i, 5)
Next j
i = i + 1
Next i
End Sub
私はエラーが新しい見つけられたセルのアドレスを配列の値を使用しようとしてもらいます。コードの何が間違っていますか?
ご協力いただきありがとうございます。
これは宿題のタスクhttp://stackoverflow.com/questions/38819421/vba-determine-if-two-or-more-cells-overlap-in-the-same-cellように感じ始めています-address/38826947#38826947 – Ambie