Looping through 2D array prints the same value issue

Toffes

New Member
Joined
Jul 15, 2016
Messages
25
I'm writing this VBA code that loops through a defined range and finds the filled cells within in, then it refers 5 different indexes to each cell, and to check if each cell is truly indexed with these values, I write my array to a worksheet.
I can manage the loop to work and print exactly the number of filled cells - but the problem is that it prints out the same exact value for each cell even though they are totally different.
Here is how my worksheet looks like:
hupqmb.jpg

And what it prints out is:
ev1a0w.jpg

Using this code:
Code:
Sub FindMol()

Dim lMolecules As Integer
lMolecules = 5   'in this case, number of filled cells

Dim Molecules() As Variant
ReDim Molecules(1 To lMolecules, 1 To 5)     'this is a 2d array to fill in a table of number of molecules x 5 indexes


Dim ColInd As Integer


Dim c As Range


For i = 1 To UBound(Molecules, 1)   'number of molecules
  For j = 1 To UBound(Molecules, 2)   '5 indexes: x,y,dx,dy,color
  
  Randomize
       dX = Int((H - L + 1) * Rnd() + L)   'speed vector x
        dY = Int((H - L + 1) * Rnd() + L)    'speed vector y
   


         'moves thorugh the defined range, in this case 10x10


     For Each c In Worksheets("Main Screen").Range(Cells(1, 1), Cells(lHeight, lWidth))  
       If c.Interior.ColorIndex <> xlNone Then
        ColInd = c.Interior.ColorIndex
     
         'printing the values found to the worksheet


         Molecules(i, 1) = i   'giving each molecule a number in order
         Worksheets("Parameters").Range("C2:" & Cells(i + 1, 3).Address) = Molecules(i, 1)
         Molecules(j, 1) = c.Column
         Worksheets("Parameters").Range("D2:" & Cells(lMolecules + 1, 4).Address) = Molecules(j, 1)
         Molecules(j, 2) = c.Row
         Worksheets("Parameters").Range("E2:" & Cells(lMolecules + 1, 5).Address) = Molecules(j, 2)
         Molecules(j, 3) = dX
         Worksheets("Parameters").Range("F2:" & Cells(lMolecules + 1, 6).Address) = Molecules(j, 3)
         Molecules(j, 4) = dY
         Worksheets("Parameters").Range("G2:" & Cells(lMolecules + 1, 7).Address) = Molecules(j, 4)
         Molecules(j, 5) = ColInd
         Worksheets("Parameters").Range("H2:" & Cells(lMolecules + 1, 8).Address) = Molecules(j, 5)     


End If
Next


Next j
Next i


End Sub

As you can see, even though the code loops through the entire range - only the last cell, Cells(6,8) is printed with its indexes.
NOTE: I can only do it using a 2D array of number of molecules x 5 indexes (uni project) so please do not try to change it, I just need to understand the problem of why I am only printing out one value for all cells.

I'd appreciate any help.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,

I tried to run your code but there are some variables that are not set:
Rich (BB code):
Sub FindMol()
    Dim lMolecules   As Long
    Dim Molecules()  As Variant
    Dim ColInd       As Long
    Dim c            As Range
    
    Dim i            As Long
    Dim j            As Long
    Dim H            As Long
    Dim L            As Long
    Dim dX           As Long
    Dim dY           As Long
    Dim lHeight      As Long
    Dim lWidth       As Long
     
    lMolecules = 5   'in this case, number of filled cells
    ReDim Molecules(1 To lMolecules, 1 To 5)     'this is a 2d array to fill in a table of number of molecules x 5 indexes
    Randomize
    For i = 1 To UBound(Molecules, 1)   'number of molecules
        For j = 1 To UBound(Molecules, 2)   '5 indexes: x,y,dx,dy,color
            dX = Int((H - L + 1) * Rnd() + L)   'speed vector x
            dY = Int((H - L + 1) * Rnd() + L)    'speed vector y
            'moves thorugh the defined range, in this case 10x10
            For Each c In Worksheets("Main Screen").Range(Cells(1, 1), Cells(lHeight, lWidth))
                If c.Interior.ColorIndex <> xlNone Then
                    ColInd = c.Interior.ColorIndex
                    'printing the values found to the worksheet
                    Molecules(i, 1) = i   'giving each molecule a number in order
                    Worksheets("Parameters").Range("C2:" & Cells(i + 1, 3).Address) = Molecules(i, 1)
                    Molecules(j, 1) = c.Column
                    Worksheets("Parameters").Range("D2:" & Cells(lMolecules + 1, 4).Address) = Molecules(j, 1)
                    Molecules(j, 2) = c.Row
                    Worksheets("Parameters").Range("E2:" & Cells(lMolecules + 1, 5).Address) = Molecules(j, 2)
                    Molecules(j, 3) = dX
                    Worksheets("Parameters").Range("F2:" & Cells(lMolecules + 1, 6).Address) = Molecules(j, 3)
                    Molecules(j, 4) = dY
                    Worksheets("Parameters").Range("G2:" & Cells(lMolecules + 1, 7).Address) = Molecules(j, 4)
                    Molecules(j, 5) = ColInd
                    Worksheets("Parameters").Range("H2:" & Cells(lMolecules + 1, 8).Address) = Molecules(j, 5)
                End If
            Next
        Next j
    Next i
End Sub
I have changed your Integers into Longs as well. Integers only work between -32,768 and 32,767.

If you let me know what the missing values should be I can have another go at trying it out.


Regards,
 
Upvote 0
Hi,

I tried to run your code but there are some variables that are not set:

I have changed your Integers into Longs as well. Integers only work between -32,768 and 32,767.

If you let me know what the missing values should be I can have another go at trying it out.


Regards,

Hello,
I forgot to mention them in the code I posted, but as I said - the code works, there is no error of Dim missing or anything, just that the loops doesn't do what I expect it to do.
L = -5
H = 5
lWidth = 10
lHeight = 10
 
Last edited:
Upvote 0
I don't know.

I think you may have a loop too many in there somewhere.

It sets i and j equal to 1 then it runs round all of the 100 cells calculating a new value for each filled cell.

The answer then overwrites the answer for the previous filled cell because i and j have not changed yet, just c.

Having overwritten the cells 5 times it advances j to 2 and overwrites everything on row 2 twice.

After it has finished all that it then it increments i and overwrites all the same things again.

The calculation block is being executed 2500 times. Does that sound right?

If you take the If Statement into account then this code is run a total of 125 times:
Code:
                    ColInd = c.Interior.ColorIndex
                    'printing the values found to the worksheet
                    Molecules(i, 1) = i   'giving each molecule a number in order
                    Worksheets("Parameters").Range("C2:" & Cells(i + 1, 3).Address) = Molecules(i, 1)
                    Molecules(j, 1) = c.Column
                    Worksheets("Parameters").Range("D2:" & Cells(lMolecules + 1, 4).Address) = Molecules(j, 1)
                    Molecules(j, 2) = c.Row
                    Worksheets("Parameters").Range("E2:" & Cells(lMolecules + 1, 5).Address) = Molecules(j, 2)
                    Molecules(j, 3) = dX
                    Worksheets("Parameters").Range("F2:" & Cells(lMolecules + 1, 6).Address) = Molecules(j, 3)
                    Molecules(j, 4) = dY
                    Worksheets("Parameters").Range("G2:" & Cells(lMolecules + 1, 7).Address) = Molecules(j, 4)
                    Molecules(j, 5) = ColInd
                    Worksheets("Parameters").Range("H2:" & Cells(lMolecules + 1, 8).Address) = Molecules(j, 5)


Regards,
 
Last edited:
Upvote 0
I don't know.

I think you may have a loop too many in there somewhere.

It sets i and j equal to 1 then it runs round all of the 100 cells calculating a new value for each filled cell.

The answer then overwrites the answer for the previous filled cell because i and j have not changed yet, just c.

Having overwritten the cells 5 times it advances j to 2 and overwrites everything on row 2 twice.

After it has finished all that it then it increments i and overwrites all the same things again.

The calculation block is being executed 2500 times. Does that sound right?

If you take the If Statement into account then this code is run a total of 125 times:
Code:
                    ColInd = c.Interior.ColorIndex
                    'printing the values found to the worksheet
                    Molecules(i, 1) = i   'giving each molecule a number in order
                    Worksheets("Parameters").Range("C2:" & Cells(i + 1, 3).Address) = Molecules(i, 1)
                    Molecules(j, 1) = c.Column
                    Worksheets("Parameters").Range("D2:" & Cells(lMolecules + 1, 4).Address) = Molecules(j, 1)
                    Molecules(j, 2) = c.Row
                    Worksheets("Parameters").Range("E2:" & Cells(lMolecules + 1, 5).Address) = Molecules(j, 2)
                    Molecules(j, 3) = dX
                    Worksheets("Parameters").Range("F2:" & Cells(lMolecules + 1, 6).Address) = Molecules(j, 3)
                    Molecules(j, 4) = dY
                    Worksheets("Parameters").Range("G2:" & Cells(lMolecules + 1, 7).Address) = Molecules(j, 4)
                    Molecules(j, 5) = ColInd
                    Worksheets("Parameters").Range("H2:" & Cells(lMolecules + 1, 8).Address) = Molecules(j, 5)


Regards,


Is there anyone who can point out the problem? I'm clueless.
 
Upvote 0
Toffee,

Does the below help?

Code:
Dim lMolecules As Integer
lMolecules = 5   'in this case, number of filled cells
Dim Molecules() As Variant
ReDim Molecules(1 To lMolecules, 1 To 5)     'this is a 2d array to fill in a table of number of molecules x 5 indexes




Dim ColInd As Integer




Dim c As Range


'*******
H = 5
L = 5
lHeight = 10
lWidth = 10
'*******


'looping just the once to calculate dx & dy ????


For i = 1 To UBound(Molecules, 1)   'number of molecules
 Molecules(i, 1) = i
  For j = 1 To UBound(Molecules, 2)   '5 indexes: x,y,dx,dy,color
  
  Randomize
       dX = Int((H - L + 1) * Rnd() + L) 'speed vector x
        dY = Int((H - L + 1) * Rnd() + L)   'speed vector y
  
  Molecules(i, 3) = dX
  Molecules(i, 4) = dY
  Next j
  
Next i
         'moves thorugh the defined range, in this case 10x10




     For Each c In Worksheets("Main Screen").Range(Cells(1, 1), Cells(lHeight, lWidth))
       If c.Interior.ColorIndex <> xlNone Then
       r = r + 1
        ColInd = c.Interior.ColorIndex
     
         'printing the values found to the worksheet




          'giving each molecule a number in order
         Worksheets("Parameters").Range("C2").Offset(r - 1, 0) = Molecules(r, 1)
         Molecules(r, 1) = c.Column
         Worksheets("Parameters").Range("D2").Offset(r - 1, 0) = c.Column
         Molecules(r, 2) = c.Row
         Worksheets("Parameters").Range("E2").Offset(r - 1, 0) = c.Row
         Worksheets("Parameters").Range("F2").Offset(r - 1, 0) = Molecules(r, 3)
         Worksheets("Parameters").Range("G2").Offset(r - 1, 0) = Molecules(r, 4)
         Molecules(r, 5) = ColInd
         Worksheets("Parameters").Range("H2").Offset(r - 1, 0) = ColInd




      End If
    Next c




End Sub


Like RickXL I am confused as to how you are wishing to iterate your calculations, especially re dx and dy.
Maybe you can clarify, in words?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top