Any way to grab a .csv file from a folder using Wildcards

D

David Johnston

Does anyone know of a way that I can grab a .csv file out of a local
folder with out specifying the exact document. I have created a macro
that can take the CSV data and import into the Excel worksheet save it
and print it but only if I code it to that exact CSV document. I was
hoping to be able to just have it grab any CSV document in a
particular folder. There will be only one CSV document at anytime in
the folder but it will not always be named the same. Any ideas.
 
D

Dan E

David,

You can use
inFile = Application.GetOpenFilename
which will open a standard "Open" file dialog box to browse for your .csv
file and returns a file name and path
ie (inFile = "C:\My Documents\MyCSV.csv")

Dan E
 
D

Dan E

Here's another option,

This one has a fixed folder to look for the csv file in

Private Sub CommandButton1_Click()
On Error GoTo Endit
Dim fs, f, f1, fc, s
folderspec = "C:\My Documents"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
i = 0
For Each f1 In fc
If Right(f1.Name, 3) = "csv" Then
inFileName = f1.ParentFolder.Path & "\" & f1.Name
i = i + 1
End If
Next
ret = MsgBox(i & " CSV file(s) found ," & inFileName & " used",
vbOKOnly)

Endit:
If i = 0 Then
ret = MsgBox("No CSV file Found", vbOKOnly, "Bad Input")
End If
End Sub

Dan E
 
L

Lynn Schauer

if there is only one csv file in the directory, try the following

f=dir("c:\my folder\*.csv")

at this point f will be the filename including the path,

Lynn S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top