ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Opening a file (https://www.excelbanter.com/excel-worksheet-functions/103652-opening-file.html)

dan

Opening a file
 
Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks

Ron de Bruin

Opening a file
 
Hi Dan

See the VBA help for Application.GetOpenFilename


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Dan" wrote in message ...
Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks




Dave Peterson

Opening a file
 
Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum





Dan wrote:

Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks


--

Dave Peterson

dan

Opening a file
 
Thanks - works great!

"Dave Peterson" wrote:

Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum





Dan wrote:

Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks


--

Dave Peterson


dan

Opening a file
 
Is there a way to automatically run the File Wizard on the imported file with
the spacing I want. (The files that will be imported will always appear the
same).

Thanks

"Dave Peterson" wrote:

Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum





Dan wrote:

Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks


--

Dave Peterson


Dave Peterson

Opening a file
 
No, but you could record a macro that parses the file when you do it once.

Then modify that code a bit.

I'd put the code into a separate workbook and drop a giant button from the Forms
toolbar onto the only worksheet in that workbook. Assign your macro to that
button.

Kind of...

Option Explicit
Sub Testme01()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub

Dan wrote:

Is there a way to automatically run the File Wizard on the imported file with
the spacing I want. (The files that will be imported will always appear the
same).

Thanks

"Dave Peterson" wrote:

Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum





Dan wrote:

Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks


--

Dave Peterson


--

Dave Peterson

dan

Opening a file
 
Thanks for the input I will try it out.

"Dave Peterson" wrote:

No, but you could record a macro that parses the file when you do it once.

Then modify that code a bit.

I'd put the code into a separate workbook and drop a giant button from the Forms
toolbar onto the only worksheet in that workbook. Assign your macro to that
button.

Kind of...

Option Explicit
Sub Testme01()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub

Dan wrote:

Is there a way to automatically run the File Wizard on the imported file with
the spacing I want. (The files that will be imported will always appear the
same).

Thanks

"Dave Peterson" wrote:

Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum





Dan wrote:

Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks

--

Dave Peterson


--

Dave Peterson


dan

Opening a file
 
Thanks. I'll try it.

"Dave Peterson" wrote:

No, but you could record a macro that parses the file when you do it once.

Then modify that code a bit.

I'd put the code into a separate workbook and drop a giant button from the Forms
toolbar onto the only worksheet in that workbook. Assign your macro to that
button.

Kind of...

Option Explicit
Sub Testme01()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub

Dan wrote:

Is there a way to automatically run the File Wizard on the imported file with
the spacing I want. (The files that will be imported will always appear the
same).

Thanks

"Dave Peterson" wrote:

Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum





Dan wrote:

Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks

--

Dave Peterson


--

Dave Peterson



All times are GMT +1. The time now is 05:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com