import only files with a certain extension

  • Thread starter Thread starter kmcosta
  • Start date Start date
K

kmcosta

I need help trying to import only files with a given extension. I dont
need to consider the text string that comes before the extension. For
example: I have a file named 1GC18950.057, but I only want to look at
the .057 for this file in a known folder. How do I change the code
below to only look for the extension and not the text string before it.
Any help will be GREATLY appreciated!!

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and
Settings\Kevin\Desktop\data\1GC18950.057",
 
You have to provide a full file name in the connection string, so you need
some way to determine that before. One way :

Dim Filename as string
Const DirSearch as string="C:\Documents and Settings\Kevin\Desktop\data\"

filename=dir(DirSearch & "*.057")
if filename<>"" then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & DirSearch & filename,..etc

Else
msgbox "No .057 files"
end if

Or look into Application.GetOpenFilename for more flexibility.

NickHK
 

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

Back
Top