Consulting

Results 1 to 6 of 6

Thread: Solved: Looping through a two dimensional array, displaying element values, having problems

  1. #1

    Solved: Looping through a two dimensional array, displaying element values, having problems

    What i am trying to do is loop through a two dimensional array that takes values from their respective cell locations and then display them one at a time in a message box. This simple code works fine until the last iteration is executed. After the last iteration it says that i have "a type mismatch error". any help would be greatly appreciated.

    -----here is the code-------

    [VBA]Sub arrayfun()
    Dim arraytest(1 To 5, 1 To 2) As Double
    For j = LBound(arraytest) To UBound(arraytest)
    For i = LBound(arraytest) To UBound(arraytest)
    arraytest(i, j) = ThisWorkbook.Sheets("sheet1").Cells(i, j).Value
    MsgBox (arraytest(i, j))
    Next
    Next
    End Sub[/VBA]
    Last edited by Aussiebear; 01-19-2013 at 03:40 PM. Reason: Added the correct tags to the supplied code

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    try:[vba]Sub arrayfun()
    Dim arraytest(1 To 5, 1 To 2) ' As Double
    For j = LBound(arraytest, 2) To UBound(arraytest, 2)
    For i = LBound(arraytest) To UBound(arraytest)
    arraytest(i, j) = ThisWorkbook.Sheets("sheet11").Cells(i, j).Value
    MsgBox arraytest(i, j) & " i=" & i & ", j=" & j
    Next
    Next
    End Sub
    [/vba] I commented out the As Double because if there's text in the cell you'll get your type mismatch. Now it'll accept anything.
    You would later get a subscript out of range error because both loops would be iterating from 1 to 5. Correction for that takes the form of the red 2s
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3

    Smile Thank you!

    Thanks for the informative and quick reply. i think i get it now.

    very well done.

  4. #4
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    In this case, just to set the array, it would be simpler to use a variant and assign the range values directly to it.

    [vba]Sub QuickArray()
    Dim arraytest 'becomes variant type
    arraytest = ThisWorkbook.Sheets("sheet11").Range("A1:B5")
    MsgBox arraytest(1, 1) ' Array is r5 x c2 per your example
    End Sub[/vba]
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    [VBA]sub M_snb()
    sn=ThisWorkbook.Sheets("sheet1").Range("A1:B5")

    for each it in sn
    msgbox it
    next
    end Sub[/VBA]

  6. #6
    thank you for all your help everyone. i am relatively new to programing and love how everyone in the community is so helpful, supportive and progressively trying to move the whole community forward.

Posting Permissions

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