Multi-dimension array

Kdbailey

Board Regular
Joined
Aug 1, 2012
Messages
156
I am having a hard time gettin my multi-dimension array to work. The basic idea of what I am doing is collecting 4 pieces of information to go with the first dimension. What it is doing is looping through, and adding new items to the array, and I do not know what the final count of items will be so I need the redim preserve the array. This code gives me the correct information for (0,0), (0,1), (0,2), (0,3) but then as soon as it tries for (1,0) the subscript is out of range. I do not fully understand how to redimension a multi-dimension array.

a is jumping down to specific rows, b is jumping over to specific columns


Code:
Private Sub CommandButton3_Click()Dim start_row6 As Integer, start_row3 As Integer, start_colcost As Integer, start_colinc As Integer
Dim sheet As Variant
Dim ws As Worksheet
Dim list1() As Variant
Dim a As Integer, b As Integer, r As Integer, index As Integer, index2 As Integer


sheet = Array("Hawk", "I2", "I3", "GE V4", "GE V6", "TBIRD", "BAT")
index = 0


For r = 0 To 6
Set ws = ActiveWorkbook.Sheets(sheet(r))


For a = 0 To 14
    
    start_row3 = 3 + (a * 44)


    For b = 0 To 1
    
        index2 = 3
        start_colcost = 9 + (b * 12)
        
        ReDim Preserve list1(index, index2)
        index2 = 0
        
        list1(index, index2) = ws.Cells(start_row3 + 1, start_colcost - 7)
        index2 = index2 + 1
        
        list1(index, index2) = ws.Cells(start_row3 + 1, start_colcost - 6)
        index2 = index2 + 1
        
        list1(index, index2) = ws.Cells(start_row3 + 1, start_colcost - 3)
        index2 = index2 + 1
        
        list1(index, index2) = ws.Cells(start_row3, start_colcost)
        
        index = index + 1
        
    Next b


    For b = 2 To 4
    
        start_colcost = 26 + (b * 12)
        index = index + 1
        ReDim Preserve list1(3, index)
        
        list1(0, index) = ws.Cells(start_row3 + 1, start_colcost - 7)
        list1(1, index) = ws.Cells(start_row3 + 1, start_colcost - 6)
        list1(2, index) = ws.Cells(start_row3 + 1, start_colcost - 3)
        list1(3, index) = ws.Cells(start_row3, start_colcost)


    Next b
Next a
Next r


End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Still not working, but the b loop is switched to


Code:
     index2 = 3

        For b = 0 To 1    
        
        start_colcost = 9 + (b * 12)
        
        ReDim Preserve list1(index, index2)
        
        list1(index, 0) = ws.Cells(start_row3 + 1, start_colcost - 7)
      
        
        list1(index, 1) = ws.Cells(start_row3 + 1, start_colcost - 6)
        
        
        list1(index, 2) = ws.Cells(start_row3 + 1, start_colcost - 3)
        
        
        list1(index, 3) = ws.Cells(start_row3, start_colcost)
        
        index = index + 1
        
    Next b
 
Upvote 0
Ya I just saw that, I fixed it up, and it runs, but now I am having a problem, I put a stop at the end of my b loop to track list1. Some reason when b finished through 1 and a moves to 1, my index jumps up multiple digits. On the 1st play, index = 1 at the end of the loop, 2 after the next, and 6 after the 3rd click. I don't know why, but its putting many different cell data types into what is supposed to be specific types.
 
Upvote 0
For instance, list1(0,index) should always equal "Hawk" on the first page. But through 3 clicks with a stop at "Next b" I am getting,
list1(0,0) = "Hawk" (first click)
list1(0,1) = "Hawk" (2nd click)
list1(0,2) = "6 Month Difference" (3rd click)
list1(0,3) = "6 Month difference" (3rd click)
list1(0,4) = "Empty" (3rd click)
list1(0,5) = "Hawk" (3rd click)

(0,5) looks like it is the right data for (0,2) but I don't know where the middle 3 are coming from.

Code:
Private Sub CommandButton3_Click()Dim start_row6 As Integer, start_row3 As Integer, start_colcost As Integer, start_colinc As Integer
Dim sheet As Variant
Dim ws As Worksheet
Dim list1() As Variant
Dim a As Integer, b As Integer, r As Integer, index As Integer, index2 As Integer


sheet = Array("Hawk", "I2", "I3", "GE V4", "GE V6", "TBIRD", "BAT")
index = 0
index2 = 3


For r = 0 To 6


Set ws = ActiveWorkbook.Sheets(sheet(r))


For a = 0 To 14
    
    start_row3 = 3 + (a * 44)


    For b = 0 To 1
    
        
        start_colcost = 9 + (b * 12)
        
        ReDim Preserve list1(index2, index)
        
        list1(0, index) = ws.Cells(start_row3 + 1, start_colcost - 7)
      
        
        list1(1, index) = ws.Cells(start_row3 + 1, start_colcost - 6)
        
        
        list1(2, index) = ws.Cells(start_row3 + 1, start_colcost - 3)
        
        
        list1(3, index) = ws.Cells(start_row3, start_colcost)
        
        index = index + 1
        
    Next b


    For b = 2 To 4
    
        start_colcost = 26 + (b * 13)
        ReDim Preserve list1(index2, index)
        
        list1(0, index) = ws.Cells(start_row3 + 1, start_colcost - 7)
      
        
        list1(1, index) = ws.Cells(start_row3 + 1, start_colcost - 6)
        
        
        list1(2, index) = ws.Cells(start_row3 + 1, start_colcost - 3)
        
        
        list1(3, index) = ws.Cells(start_row3, start_colcost)
        
        index = index + 1
    Next b
Next a
Next r


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,449
Messages
6,124,911
Members
449,195
Latest member
Stevenciu

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