Opening a text file using a macro
Opening a text file using a macro
(OP)
I have a text file as output from a live load generator. I will always put the file in the following directory:
C:\LoadRating\
Say I have a text file called "HS20.txt" in the C:\LoadRating\ folder. I want to put "HS20.txt" in Excel cell A1 and then have Excel open that file. How do I write the macro such that it opens the file that I specify in cell A1? Is this even possible?
Thanks!
C:\LoadRating\
Say I have a text file called "HS20.txt" in the C:\LoadRating\ folder. I want to put "HS20.txt" in Excel cell A1 and then have Excel open that file. How do I write the macro such that it opens the file that I specify in cell A1? Is this even possible?
Thanks!





RE: Opening a text file using a macro
Option Explicit
Sub Tampa1()
Dim inFile As String
inFile = Range("inputFile")
Workbooks.Open (inFile)
End Sub
You may have to tweak things so that the text file is parsed as you like it when it is opened in Excel. You might find something like the VBA "Input" or "Line Input" commands useful (reads data from text file and you can parse within the VBA code).
RE: Opening a text file using a macro
Cheers
Greg Locock
Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
RE: Opening a text file using a macro
I've recorded my actions as a macro many times before but I've never had a situation like this where I could have up to 100 different files that I need to open. The goal was to be able to put the name of the file I want to open in cell A1 and then press a button and have that file open.
RE: Opening a text file using a macro
Workbboks.OpenText sFilename
where sFilename is the full path and file name of the path to open
If you dont know the name of the file to open put the following line of code before the Workbooks.opentext line
sFilename = Application.GetOpenFilename()
which will display to standard microsoft open file dialog and allow the user to select the file to open.
RE: Opening a text file using a macro
CODE
sFilename = Cells(1, 1)
Workbooks.OpenText sFilename
End Sub
Good Luck
johnwm
________________________________________________________
To get the best from these forums read FAQ731-376 before posting
Steam Engine enthusiasts: www.essexsteam.co.uk
RE: Opening a text file using a macro
Griffy
RE: Opening a text file using a macro
That's exactly what I was looking for! I'll just have to modify it slightly to align the columns. Thanks for your help with this!
RE: Opening a text file using a macro
Option Explicit
Sub ReadFile()
Dim inFile As String, Folder As String
Folder = "LoadRating"
inFile = Cells(1, 1).Value
inFile = "C:\" & Folder & "\" & inFile
Open inFile For Input As #1
.
.
Create a button in a worksheet that is assigned to
macro ReadFile
RE: Opening a text file using a macro
Thanks for your response. What you have there will give me a little more flexibility.
Thanks to everyone who responded to my request!!
RE: Opening a text file using a macro