Consulting

Results 1 to 5 of 5

Thread: Two dimensional arrays

  1. #1

    Two dimensional arrays

    Hi All,

    I am working on a script, where I want to hold two sets of data in an array or any other form. The first one is a variable integer and the second one is a string. Can I use a two-dimensional array? How do I declare and assign the variables to each element?
    I am now using two one-dimensional arrays.

    - Krishna

  2. #2
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    you would declare a two dimentional array like this

    [VBA]dim a(0 to 1,0 to 65) as String[/VBA]

    but it can only have one varible type. so if you need two diffent types, then you best bet is to use two arrays.

    storing values to your array is dont like

    [VBA]a(0,44)="b"[/VBA]

    retreaving data would be

    [VBA]C = a(0,44)[/VBA]

    you can go as many dimentions as you need.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by figment
    but it can only have one varible type. so if you need two diffent types, then you best bet is to use two arrays.
    That is not so

    [vba]

    Sub Load2DArray()
    Dim myArray(1 To 2, 1 To 2)
    Dim i As Long

    myArray(1, 1) = "Bob"
    myArray(1, 2) = 300
    myArray(2, 1) = "figment"
    myArray(2, 2) = 500

    For i = 1 To 2
    MsgBox myArray(i, 1) & " - " & myArray(i, 2)
    Next i

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Krishna

    Declare the array as Variant then it can hold both numbers and strings.

    Even if you declared it as String then you could still work with numbers.

    They would be stored in the array as strings so if you wanted to perform any calculations you would need to convert them to numbers.

    A little more work but perfectly feasible - VBA has various conversion functions. eg Val, CInt, CDbl etc

  5. #5
    Hi All,

    Thanks for all the suggestions. I am now working with two arrays but I'll give it a try with a single array declared as variant.

    - Krishna

Posting Permissions

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