Results 1 to 3 of 3

Thread: How to Receive an array? *resolved*

  1. #1

    Thread Starter
    Member superfrog80's Avatar
    Join Date
    Aug 2003
    Location
    Ma-Lai-Xi-Ya
    Posts
    32

    How to Receive an array? *resolved*

    Hi!

    I'm trying to code some macros in Excel 2k...

    The basic idea is to open/load several *.csv files, then import the data into a chart...
    I've been able to load a single *.csv file using the GetOpenFilename method...

    but I'm having problems with opening multiple files...
    I've set the MultiSelect argument in the GetOpenFilename method to "True", which allows me to select multiple files...
    but how do i open all the files?
    coz the GetOpenFilename returns the paths as an array...
    ... how do i store a "returned array"??

    (currently, for the individual files, i store the string from GetOpenFilename, then use the Open method to open the *.csv file in another workbook.... is this a "stupid" way of doing it?... is there a more efficient way?)

    anyway... here's my half-chewed code...
    VB Code:
    1. Option Explicit
    2. Private Sub cmdLoadFile_Click()
    3. Dim strLoadFile As String
    4.     strLoadFile = Application.GetOpenFilename("CSV Files (*.csv),*.csv,All Files (*.*),*.*", , "Open")
    5.     If strLoadFile <> "False" Then
    6.         Application.Workbooks.Open strLoadFile
    7.         ThisWorkbook.Sheets("Chart1").Activate
    8.         ThisWorkbook.Sheets("Chart1").ChartObjects(1).Activate
    9.         ActiveChart.SetSourceData Source:=Workbooks(2).Sheets(1).Range("a1:a10"), PlotBy:=xlColumns
    10.    End If
    11. End Sub
    any and all input/comments readily welcome..
    ;p
    Last edited by superfrog80; Oct 13th, 2003 at 07:54 PM.

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    There are more efficient ways if you know something about the files that you need to open, but this works if you need to have the user select the files. The result from GetOpenFilename can be a string ("False" if user presses cancel) or an array. So you need it to return to a Variant. If the return value is not an array, the the user didn't select any files to open. Otherwise, you can loop through the array to open the files.

    VB Code:
    1. Private Sub cmdLoadFile_Click()
    2.    
    3.     ' Use a variant because return value could be an array or string "False".
    4.     Dim strLoadFile As Variant
    5.    
    6.     ' Let user select multiple files.
    7.     strLoadFile = Application.GetOpenFilename("CSV Files (*.csv),*.csv,All Files (*.*),*.*", , "Open", , True)
    8.    
    9.     ' If return value isn't an array, no files were selected to open.
    10.     If Not IsArray(strLoadFile) Then Exit Sub
    11.    
    12.     ' Open all files in array.
    13.     For i = 1 To UBound(strLoadFile)
    14.         Application.Workbooks.Open strLoadFile(i)
    15.     Next i
    16.    
    17. End Sub

  3. #3

    Thread Starter
    Member superfrog80's Avatar
    Join Date
    Aug 2003
    Location
    Ma-Lai-Xi-Ya
    Posts
    32
    aahhh....

    NOW i know wat a variant's for...
    thanx a lot WorkHorse!!...
    helped me earn this months' pay!!..hahaha...
    woohoo!!....
    ;p

Posting Permissions

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



Click Here to Expand Forum to Full Width