Consulting

Results 1 to 15 of 15

Thread: array in vba

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    9
    Location

    array in vba

    Hi,

    Is it possible to fill an array (variant type) without knowing its size ?
    for instance, in c++ you have push_back() from stl, is there a solution in vba ?

    thanks in advance

  2. #2
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    Check out the ReDim statement.

  3. #3
    VBAX Regular
    Joined
    Aug 2009
    Posts
    9
    Location
    Quote Originally Posted by nst1107
    Check out the ReDim statement.
    I have seen, but I will not resize each time I had a string .. isnt it dirty ?

  4. #4
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    That's the only way I know to fill an array of undetermined size.

  5. #5
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    Alternatively, you can use the collection or dictionary objects.

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Where's the data coming from?
    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.

  7. #7
    VBAX Regular
    Joined
    Aug 2009
    Posts
    9
    Location
    Im listing the files in a folder to an array

  8. #8
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Well, do you have Excel 2003?

    [VBA]Option Explicit
    Option Base 1
    Sub ListFolders()
    Dim n As Long
    Dim arFolders() As String

    With Application.FileSearch
    .LookIn = "C:\Documents and Settings\felipe.gualberto\Desktop\Felipe\VBA"
    .FileType = msoFileTypeAllFiles
    .Execute
    ReDim arFolders(.FoundFiles.Count)

    For n = 1 To .FoundFiles.Count
    arFolders(n) = .FoundFiles(n)
    Next n

    End With
    Debug.Print UBound(arFolders)
    End Sub[/VBA]

  9. #9
    VBAX Regular
    Joined
    Aug 2009
    Posts
    9
    Location
    Quote Originally Posted by Benzadeus
    Well, do you have Excel 2003?

    [vba]Option Explicit
    Option Base 1
    Sub ListFolders()
    Dim n As Long
    Dim arFolders() As String

    With Application.FileSearch
    .LookIn = "C:\Documents and Settings\felipe.gualberto\Desktop\Felipe\VBA"
    .FileType = msoFileTypeAllFiles
    .Execute
    ReDim arFolders(.FoundFiles.Count)

    For n = 1 To .FoundFiles.Count
    arFolders(n) = .FoundFiles(n)
    Next n

    End With
    Debug.Print UBound(arFolders)
    End Sub[/vba]
    I can not test now, but the vba code should work for every version of excel

  10. #10
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    No, the object FileSearch does not work in Excel 2007+.

  11. #11
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    but good old Dir does...
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Do you really need an array?
    [vba]
    Sub ListFolders()
    Dim arFolders As Object, a, i As Long

    With Application.FileSearch
    .LookIn = "C:\AAA"
    .FileType = msoFileTypeAllFiles
    .Execute
    Set arFolders = .FoundFiles
    End With

    For Each a In arFolders
    Debug.Print a
    Next
    'or
    For i = 1 To arFolders.Count
    Debug.Print arFolders(i)
    Next
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    VBAX Regular
    Joined
    Aug 2009
    Posts
    9
    Location
    this is in a function that returns the list of the file of a folder, so yes I think.

  14. #14
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    MD,

    [VBA] Set arFolders = .FoundFiles [/VBA]

    I didn't think that =)

    Is it possible to change
    [VBA] For Each a In arFolders
    Debug.Print a
    Next [/VBA]

    For something like
    [VBA] Debug.Print Join(arFolders, vbCrLf)[/VBA]?

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    A common technique I've seen uses Redim Preserve to keep resizing the array as you add elements

    [VBA]
    Sub Demo()
    Dim a() As Variant
    Dim n As Long, iPretendThisIsaFile As Long

    n = 0

    For iPretendThisIsaFile = 1 To 10
    n = n + 1
    ReDim Preserve a(1 To n)

    a(iPretendThisIsaFile) = "This is File #" & iPretendThisIsaFile

    Next iPretendThisIsaFile

    For n = LBound(a) To UBound(a)
    Debug.Print a(n)
    Next n
    End Sub
    [/VBA]

    Paul

Posting Permissions

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