PDA

Click to See Complete Forum and Search --> : Excel opening .txt file code?


12ory
Nov 23rd, 2005, 08:36 AM
Hi,

Can someone please help me with some simple vb code for my excel macro. I wish to open a .txt file which will have a specific name based on what is contained in a certain cell in my spreadsheet. Can this be done and if so what coding would I use? I can get the a .txt file to open based on renaming the file to a certain name but I wish to be able to tell it what the name is by typing text into a cell.

Thanks

12ory
Nov 23rd, 2005, 08:56 AM
At the moment I have

Dim projectname As String
projectname = Range("C4")
Dim referenceid As String
referenceid = Range("C5")

Workbooks.OpenText FileName:= _
"C:\Documents and Settings\rkennedy\Desktop\" & projectname & "~" & referenceid & ".142", Origin:= _
xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=False, Space:=False, Other:=True, OtherChar:="=", FieldInfo _
:=Array(Array(1, 1), Array(2, 1))

This gives an error that it cannot find the file which will have the name cell1~cell2.142

Static
Nov 23rd, 2005, 08:57 AM
Excel.Workbooks.Open "C:\path\" & Worksheets(1).Range("A1").Value

worked fine for me..

try doing it with a regular name file.. like Test.txt

Hack
Nov 23rd, 2005, 10:20 AM
Excel VBA question moved to Office Development.