Opening a file

G

Guest

Hi guys, I'm becoming increasingly frustrated with what I think is a
relatively simple procedure.

I am creating a process that requires the user to open a file (I'm not sure
where it will be saved), and then the code will simply copy some data out of
this selected file into the template document.

I currently have:

Public Sub ImportData()

Dim curfolder As String
Dim filename As String

Sheets("Sheet1").Select
Range("A1:ED1000").Select
Selection.ClearContents

Application.GetOpenFilename
filename = Dir(curfolder)

Workbooks.Open filename
Range("A1:ED1000").Select
Selection.Copy



However, when I reach the select file dialog box and select the necessary
file, the file I want isn't actually selected, but the first file in that
folder.

Can someone help me with this predicament please?

Thanks
 
A

Andy Pope

Hi,

You are mixing up two methods of getting filenames.
The GetOpenfilename displays the folder view and returns the selected
filename, or False is none is picked.
The DIR command returns filenames that match a given search string, eg:
"C:\myfolder\*.xls"


Dim Filename As Variant

Filename = Application.GetOpenFilename
If Filename = False Then
MsgBox "No file selected"
Else
MsgBox "Process file " & Filename
End If

Cheers
Andy
 
G

Guest

Cheers Andy, that works a treat.

Andy Pope said:
Hi,

You are mixing up two methods of getting filenames.
The GetOpenfilename displays the folder view and returns the selected
filename, or False is none is picked.
The DIR command returns filenames that match a given search string, eg:
"C:\myfolder\*.xls"


Dim Filename As Variant

Filename = Application.GetOpenFilename
If Filename = False Then
MsgBox "No file selected"
Else
MsgBox "Process file " & Filename
End If

Cheers
Andy
 

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