Results 1 to 4 of 4

Thread: Looping through columns and arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    23

    Looping through columns and arrays

    Hi all, new to vb scripting and trying to get work with an excel document and loop through an column on data and add it to an array

    VB Code:
    1. Dim i
    2. i = 0
    3. Dim bv
    4. Dim arrBan()
    5. Dim CellValue
    6. bv = 3
    7. 'check the length of the column to create an array of good length
    8. Do Until objXL.Cells(bv,15).value = ""
    9. arrBan(i) = objXL.Cells(bv,15).value
    10. i = i + 1
    11. bv = bv + 1
    12. loop

    from this i get a subscript out of range error, the column can be any length so i cant set a value to the array but a dynamic array should work fine right?
    also to see if my thought proccess is right, overall what i am trying to do is compare 2 excel docs to a third, based on the numbers in this column, what i was going to do was get the get the numbers from the 2 sheets and compare them to the third. long proccess i know but i am slowly working on it any advice would be greatly apprecaited

    EDIT: fixed the looping issue the array had to be set to a value, now a better question is there a way to dynamically set a array length based on a number of columns
    Last edited by fizban2; Mar 23rd, 2006 at 03:31 PM.

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Looping through columns and arrays

    You just need to use the Redim statement to size the array each time you add an element. When you Dim a dynamic array, VB doesn't actually initialize it until you give it bounds:
    VB Code:
    1. Do Until objXL.Cells(bv, 15).Value = ""
    2.     ReDim Preserve arrBan(i)
    3.     arrBan(i) = objXL.Cells(bv, 15).Value
    4.     i = i + 1
    5.     bv = bv + 1
    6. Loop

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    23

    Re: Looping through columns and arrays

    ok, now i make it a little more interesting,
    is there a way to use the do loop to read through multiple worksheets? the values are all in the same column i just need to collect them into an array or something so that i can work with later

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Looping through columns and arrays

    Something like this should do the trick:
    VB Code:
    1. Dim oBook As Workbook, oSheet As Worksheet, i As lonb, bv As Long
    2. Dim arrBan() As Variant, CellValue As Variant
    3.  
    4. Set oBook = Application.ActiveWorkbook
    5.  
    6. For Each oSheet In oBook.Worksheets
    7.     bv = 3
    8.     i = 0
    9.     Do Until objXL.Cells(bv, 15).Value = ""
    10.         ReDim Preserve arrBan(i)
    11.         arrBan(i) = objXL.Cells(bv, 15).Value
    12.         i = i + 1
    13.         bv = bv + 1
    14.     Loop
    15. Next oSheet
    16.  
    17. Set oBook = Nothing
    EDIT: You'll have to either process the resulting arrays inside of the For loop, or if you want everything in one big array, take out the i = 0 line.
    Last edited by Comintern; Mar 23rd, 2006 at 05:34 PM. Reason: Add comment

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width