2016-08-20 3 views
0

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 

私はエラーが新しい見つけられたセルのアドレスを配列の値を使用しようとしてもらいます。コードの何が間違っていますか?

ご協力いただきありがとうございます。

+0

これは宿題のタスクhttp://stackoverflow.com/questions/38819421/vba-determine-if-two-or-more-cells-overlap-in-the-same-cellように感じ始めています-address/38826947#38826947 – Ambie

答えて

0

あなたは何かを宣言して、見た目が必要であることを知る必要があります。例のように

Dim rCell as Range 
for each rCell in c.Cells 
0

が、私はそれをチェックアウトし、私のPCではないが、私は次のように気づいた:

  • For Each c In Worksheets("Main Screen")

    For Each c In Worksheets("Main Screen").Cells 
    
  • する必要があります
  • i = i + 1

    既に

  • Cells(Molecules(i, 2), Molecules(i, 1)).Interior.ColorIndex = Molecules(i, 5)

    は、それが、それが依存するすべての繰り返しで同じ結果を生成For j = 1 To UBound(Molecules, 2)ループ内で反復子を更新しているFor i = 1 To UBound(Molecules)ループ内にある、消去されるべきですi変数のみ:ループから取り除かれ、外側に配置されるように見えます。

+0

@Toffes:あなたはそれを通過しましたか? – user3598756

関連する問題