PC Review


Reply
Thread Tools Rate Thread

How can I make Excel read a value from a textfile and assign it to a variable in my VBA code??

 
 
haakon
Guest
Posts: n/a
 
      27th Feb 2007
I have a bunch of textfiles that I import to excel with some macros I
have recorded/written. There are 4 different types of textfiles, so
the macro reads the content of cell M1, e.g. "TYPE 1", and then
imports the textfile with the appropriate formatting for textfile type
1. This kind of works, but it would be much more clever if I could
make the following work: Line 58 of each textfile is specific to the
type of textfile. I therefore need to make Excel read the contents of
line 58 before importing the textfile, and assign the contents to a
variable. I could use this variable to define the necessary
formatting. So something like this:

read line 58 of textfile.txt -> assign the contents of the line to
variable TYPE

Select case TYPE
case "this is a type1 file"
formatoptions 1
case "this is a type2 file"
formatoptions 2
..
case else
msgbox "unrecognized file"
End Select

So long story short: How do I say in VBA code that the contents of
line 58 in a specified file is to be assigned to a variable?

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      27th Feb 2007
Sub ReadStraightTextFile()
Dim sStr as String
Dim LineofText As String
Dim rw as Long
Dim fName as Variant
chDrive "C"
ChDir "C:\MyTextfiles"
fname = Application.GetOpenfilename( _
FileFilter:="Text Files (*.txt),*.txt")
If fName = False then exit sub

rw = 0
Open fName For Input As #1
Do While Not EOF(1)
Line Input #1, LineofText
rw = rw + 1
if rw = 58 then
sStr = LineofText
exit do
end if
Loop
Close #1
Select Case sStr
Case "this is type1 file"

.. . .


End Sub


--
Regards,
Tom Ogilvy

"haakon" wrote:

> I have a bunch of textfiles that I import to excel with some macros I
> have recorded/written. There are 4 different types of textfiles, so
> the macro reads the content of cell M1, e.g. "TYPE 1", and then
> imports the textfile with the appropriate formatting for textfile type
> 1. This kind of works, but it would be much more clever if I could
> make the following work: Line 58 of each textfile is specific to the
> type of textfile. I therefore need to make Excel read the contents of
> line 58 before importing the textfile, and assign the contents to a
> variable. I could use this variable to define the necessary
> formatting. So something like this:
>
> read line 58 of textfile.txt -> assign the contents of the line to
> variable TYPE
>
> Select case TYPE
> case "this is a type1 file"
> formatoptions 1
> case "this is a type2 file"
> formatoptions 2
> ..
> case else
> msgbox "unrecognized file"
> End Select
>
> So long story short: How do I say in VBA code that the contents of
> line 58 in a specified file is to be assigned to a variable?
>
>

 
Reply With Quote
 
haakon
Guest
Posts: n/a
 
      28th Feb 2007
> Open fName For Input As #1
> Do While Not EOF(1)
> Line Input #1, LineofText
> rw = rw + 1
> if rw = 58 then
> sStr = LineofText
> exit do
> end if
> Loop
> Close #1


This worked perfectly, cheers

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign a Ribbon Control's Label from a Variable in VBA Code jean grey Microsoft Excel Programming 3 27th Jan 2009 05:47 AM
How to assign the value of the java script variable to a server side variable in C# code behind code? ravindravarman@gmail.com Microsoft Dot NET 1 5th Apr 2007 04:04 PM
How to assign the value of the java script variable to a server side variable in C# code behind code? ravindravarman@gmail.com Microsoft Dot NET 0 5th Apr 2007 07:58 AM
Lookp Acrobat install location in code and assign to a variable Ken Microsoft Access 4 17th Sep 2006 06:13 PM
Lookp Acrobat install location in code and assign to a variable Ken Microsoft Access Form Coding 2 17th Sep 2006 04:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:09 PM.