|
-
Oct 13th, 2003, 03:02 AM
#1
Thread Starter
Member
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:
Option Explicit
Private Sub cmdLoadFile_Click()
Dim strLoadFile As String
strLoadFile = Application.GetOpenFilename("CSV Files (*.csv),*.csv,All Files (*.*),*.*", , "Open")
If strLoadFile <> "False" Then
Application.Workbooks.Open strLoadFile
ThisWorkbook.Sheets("Chart1").Activate
ThisWorkbook.Sheets("Chart1").ChartObjects(1).Activate
ActiveChart.SetSourceData Source:=Workbooks(2).Sheets(1).Range("a1:a10"), PlotBy:=xlColumns
End If
End Sub
any and all input/comments readily welcome..
;p
Last edited by superfrog80; Oct 13th, 2003 at 07:54 PM.
-
Oct 13th, 2003, 07:34 PM
#2
Fanatic Member
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:
Private Sub cmdLoadFile_Click()
' Use a variant because return value could be an array or string "False".
Dim strLoadFile As Variant
' Let user select multiple files.
strLoadFile = Application.GetOpenFilename("CSV Files (*.csv),*.csv,All Files (*.*),*.*", , "Open", , True)
' If return value isn't an array, no files were selected to open.
If Not IsArray(strLoadFile) Then Exit Sub
' Open all files in array.
For i = 1 To UBound(strLoadFile)
Application.Workbooks.Open strLoadFile(i)
Next i
End Sub
-
Oct 13th, 2003, 07:54 PM
#3
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|