Consulting

Results 1 to 10 of 10

Thread: How to enumerate a variant array?

  1. #1
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location

    How to enumerate a variant array?

    Hi,

    Does anyone know why the code below raises an error?

    [vba]Sub arrArray()
    Dim myArr As Variant
    Dim a

    For i = 1 To 100
    ActiveSheet.Cells(i, 1) = i
    Next

    myArr = ActiveSheet.Range(Cells(1, 1), Cells(ActiveSheet.Rows.Count, 1).End(xlUp)).Value2

    'This code works fine
    i = 0
    For Each a In myArr
    i = i + 1
    Cells(i, 2) = a
    Next

    'What's wrong with this code
    For i = 1 To UBound(myArr)
    MsgBox myArr(i)
    Next

    End Sub
    [/vba]
    Greetz,
    Stranno

  2. #2
    [vba]
    For i = 1 To UBound(myArr)
    MsgBox myArr(i, 1)
    Next
    [/vba]

  3. #3
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Try also:
    [VBA]myArr = ActiveSheet.Range(Cells(1, 1), Cells(ActiveSheet.Rows.Count, 1).End(xlUp)).Value2[/VBA]

    to
    [VBA]myArr = Application.Transpose(ActiveSheet.Range(Cells(1, 1), Cells(ActiveSheet.Rows.Count, 1).End(xlUp)).Value2)[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  4. #4
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Thank 's snb and shrivallabha you 've refreshed my memory.
    By the way, is the transpose function still limited to 65536
    elements in the latest version of Excel?

  5. #5
    I'm very interested in the results of your test.

  6. #6
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Quote Originally Posted by stranno
    Thank 's snb and shrivallabha you 've refreshed my memory.
    By the way, is the transpose function still limited to 65536
    elements in the latest version of Excel?
    I have 2007 following works:
    [VBA]Var = Application.Transpose(Range("A1:A65536").Value)
    [/VBA]
    and following doesn't:
    [VBA]Var = Application.Transpose(Range("A1:A65537").Value)
    [/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  7. #7
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    "I'm very interested in the results of your test."

    Both solutions worked fine!

    Don't know how to mark "Solved"

    shrivallabha, you figured it out for Office 2007. I work with Office 2010 and
    get de same result. Probably a remain of the older versions. But strange it is!

  8. #8
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    snb,
    I visited http://www.snb-vba.eu and bookmarked it.
    It 's a very helpfull site!

  9. #9

  10. #10
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Quote Originally Posted by stranno
    Don't know how to mark "Solved"
    Hi stranno,

    The option can be found under "Thread Tools" which says "Mark as Solved".

    Note:= It has some browser issues as well e.g. you won't be able to see it in Google Chrome. Use Internet Explorer to see this option.
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

Posting Permissions

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