Consulting

Results 1 to 7 of 7

Thread: Solved: Problem with array

  1. #1
    VBAX Regular
    Joined
    Oct 2006
    Location
    Wiltshire, UK
    Posts
    43
    Location

    Solved: Problem with array

    [VBA]Sub ColumnHeaders()
    Dim myArray As Variant
    Dim myCount As Integer
    'Fill array
    myArray = Array("Name", "Address", "Phone", "Email")
    'Empty array
    With Worksheets("Sheet1")
    For myCount = 1 To UBound(myArray)
    .Cells(1, myCount).Value = myArray(myCount)
    Next myCount
    End With
    End Sub[/VBA]

    Hi all

    Attempting to understand arrays and have come up with the above example.

    It works fine except it is missing the first value, "Name" so what I get is
    Cell - A1, B1, C1
    Value - Address, Phone, Email

    For some reason it is ignoring "Name"?
    I am not sure what is happening here?! I have tried a few things like changing the dimension? to myCount = 1 to 4, but this causing an error as it seems to only find 3 in the array.

    Also can someone explain UBound(myArray) is doing? I have looked up UBound in the help files but cannot understand and I cant find a reference to UBound in my VBA book.

    Can someone explain this in pain english?

    Thank you very much.

    Cheers
    J

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Unless you have explicitly added

    [vba]

    Option Base 1
    [/vba]

    arrays start at 0 not 1. You should either start your index at 0, or better, cater for all possibilities

    [vba]

    Dim myArray As Variant
    Dim myCount As Integer
    'Fill array
    myArray = Array("Name", "Address", "Phone", "Email")
    'Empty array
    With Worksheets("Sheet1")
    For myCount = LBound(myArray) To UBound(myArray)
    .Cells(1, myCount).Value = myArray(myCount)
    Next myCount
    End With
    [/vba]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    UBound and LBound are the upper and lower boundaries of the array, the last and first element index.

  4. #4
    VBAX Regular
    Joined
    Oct 2006
    Location
    Wiltshire, UK
    Posts
    43
    Location
    xld

    Thank you, I though it would be as simple as that and now that you mention it, it makes sence.

    When you say 'cater for all possibilities' and you provide amended code
    [VBA]For myCount = LBound(myArray) To UBound(myArray)[/VBA]
    What do you mean? as I still needed to set the base as 1.

    Your explaination of UBound & LBound.
    Sorry but I still dont quite understand.
    UBound is the lowest value while UBound is the highest, how does this work in this example? does it count the number of values in the array and as there are 4 UBound becomes 4.

    Am I on the right line of thought? or do I not really worry about it as I have a working example?

    Again thanks for your help.

    Best wishes
    J

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No you don't have to set the base to anything, by looping from the lower to upper bounds, it will work correctly regardless of whether base 0 or base 1.

    LBouund is the lower bound, UBound is the upper.

  6. #6
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    With no "Option Base" or "Option Base =0", LBound As Long =0. Then Cells(1, Mycount) means Cells(1,0) where the idexes are Varients, but 0 is not a valid cell index.

    [VBA]
    .Cells(1, myCount +1).Value = myArray(myCount)
    [/VBA]

    Should be used.



    With "Option Base =1, Lbound = 1. This will work in your example.

    If you explicitly declare the array indexes,
    [VBA]
    Dim myArray(1 to4) As Variant
    myCount = LBound(myArray) To UBound(myArray)
    [/VBA]
    Will work.

    SamT

  7. #7
    VBAX Regular
    Joined
    Oct 2006
    Location
    Wiltshire, UK
    Posts
    43
    Location
    SamT & xld

    Thank you both.
    I am starting to understanding this better. I think I will now go away and look a bit more on the internet.

    Cheers
    Jay

Posting Permissions

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