Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Still stuck with importing a delimitted file.

Three example lines of my csv file are..

abc 123,123.4
1 0 002,900.23231
1 0 003,31.12

And it appears to recognise the spaces in the lines as delimeters as
well as the commas.

The purpose of my code is to open a text file and round values in the
second field of each line and the full code is as follows:

mycount = 1
Do While Range("a" & mycount) < ""

a = "c:\web\" & Range("a" & mycount) & ".csv"
b = "c:\web\" & Range("a" & mycount) & ".txt"
Open a For Input As #1
Open b For Output As #2

b = LCase(b)

Do Until EOF(1)
Input #1, myfirstfield, mysecondfield
mysecondfield = (Int(mysecondfield * 100)) / 100
Print #2, myfirstfield & "," & mysecondfield
Loop
Close
mycount = mycount + 1

Loop


Thanks again.

On Wed, 7 Jan 2004 23:04:25 -0600, "RWN" wrote:

Hmmm. Work fine here.
File;
123,456
789,012
"ROB,RWN","Test it"

Dim F1 as String
Dim F2 as String

Sub Testit()
Open "C:\xfer\test.csv" For Input As #1
Do Until EOF(1)
Input #1, F1, F2
Debug.Print F1, F2
Loop
Close #1
End Sub

Out
123 456
789 012
ROB,RWN Test it

Perhaps I'm missing something as to what your file looks like?

--
Regards;
Rob
------------------------------------------------------------------------
wrote in message
...
Oops... It's not actually working quite right as it's not using comma
as a delimeter. Some of the values in my first field have spaces and
it appears to be picking up those as delimeters too.
'
Any ideas on how to resolve this?

Thanks

On Thu, 08 Jan 2004 03:34:58 GMT, ) wrote:

Thanks Rob!

On Wed, 7 Jan 2004 21:35:26 -0600, "RWN" wrote:

Try this

Open "c:\myfile.csv" for Input as #1
Do until EOF(1)
Input #1,myfirstfield,mysecondfield
Loop

Close #1

--
Regards;
Rob


----------------------------------------------------------------------

--
wrote in message
...
Hi,

I do a lot of creating text files from Excel workbooks using "Open
blahblah for output as #1" and then printing to that file with

"Print
#1, "etc"..

I now need to do some input from text files and don't know the

syntax
& can't find it in HELP.

Specifically, I have a comma separated text file with an undefined
number of rows but always two fields.

What code would I need to achieve the following..

-Open a text file for reading
-Start a loop that will check line after line until the end of the
file
-Take both fields of a line of data (comma separated) and put each


field in a separate variable
-end the loop

I expect it would be something like this...(and I do know this

does
nothing at the moment. I will be doing further manipulation of the
variables & dumping to another file later).

Open "c:\myfile.csv" for input as #1
Do while not EOF(myfile.csv)
read myfirstfield,mysecondfield
loop


Any help would be much appreciated.

Thanks.









  #2   Report Post  
Posted to microsoft.public.excel.programming
RWN RWN is offline
external usenet poster
 
Posts: 104
Default Still stuck with importing a delimitted file.

Tino;
Dimension your input fields as string.
By not dimensioning them basic assumes that the variables are
"Variants", which means it will decide what format they are.
Thus, when it sees the "1 0" it assumes that it is a numeric (it thinks
the blank is the sign byte).
Here's what I did, using your file.

Dim F1 As String
Dim F2 As String

Sub Testit()
Open "C:\xfer\tino.txt" For Input As #1
Do Until EOF(1)
Input #1, F1, F2
mysecondfield = (Int(Val(F2) * 100)) / 100 *** Told basic that I wanted
it to treat the string "F2" as a numeric value.

Debug.Print F1, F2, mysecondfield
Loop
Close #1

End Sub

And here's what I got

F1 ...............F2..............mysecondfield
abc 123 123.4 123.4
1 0 002 900.23231 900.23
1 0 003 31.12 31.12


--
Regards;
Rob
------------------------------------------------------------------------
wrote in message
...
Three example lines of my csv file are..

abc 123,123.4
1 0 002,900.23231
1 0 003,31.12

And it appears to recognise the spaces in the lines as delimeters as
well as the commas.

The purpose of my code is to open a text file and round values in the
second field of each line and the full code is as follows:

mycount = 1
Do While Range("a" & mycount) < ""

a = "c:\web\" & Range("a" & mycount) & ".csv"
b = "c:\web\" & Range("a" & mycount) & ".txt"
Open a For Input As #1
Open b For Output As #2

b = LCase(b)

Do Until EOF(1)
Input #1, myfirstfield, mysecondfield
mysecondfield = (Int(mysecondfield * 100)) / 100
Print #2, myfirstfield & "," & mysecondfield
Loop
Close
mycount = mycount + 1

Loop


Thanks again.

On Wed, 7 Jan 2004 23:04:25 -0600, "RWN" wrote:

Hmmm. Work fine here.
File;
123,456
789,012
"ROB,RWN","Test it"

Dim F1 as String
Dim F2 as String

Sub Testit()
Open "C:\xfer\test.csv" For Input As #1
Do Until EOF(1)
Input #1, F1, F2
Debug.Print F1, F2
Loop
Close #1
End Sub

Out
123 456
789 012
ROB,RWN Test it

Perhaps I'm missing something as to what your file looks like?

--
Regards;
Rob


-----------------------------------------------------------------------

-
wrote in message
...
Oops... It's not actually working quite right as it's not using

comma
as a delimeter. Some of the values in my first field have spaces

and
it appears to be picking up those as delimeters too.
'
Any ideas on how to resolve this?

Thanks

On Thu, 08 Jan 2004 03:34:58 GMT, )

wrote:

Thanks Rob!

On Wed, 7 Jan 2004 21:35:26 -0600, "RWN"

wrote:

Try this

Open "c:\myfile.csv" for Input as #1
Do until EOF(1)
Input #1,myfirstfield,mysecondfield
Loop

Close #1

--
Regards;
Rob


---------------------------------------------------------------------

-
--
wrote in message
...
Hi,

I do a lot of creating text files from Excel workbooks using

"Open
blahblah for output as #1" and then printing to that file with

"Print
#1, "etc"..

I now need to do some input from text files and don't know the

syntax
& can't find it in HELP.

Specifically, I have a comma separated text file with an

undefined
number of rows but always two fields.

What code would I need to achieve the following..

-Open a text file for reading
-Start a loop that will check line after line until the end of

the
file
-Take both fields of a line of data (comma separated) and put

each

field in a separate variable
-end the loop

I expect it would be something like this...(and I do know this

does
nothing at the moment. I will be doing further manipulation of

the
variables & dumping to another file later).

Open "c:\myfile.csv" for input as #1
Do while not EOF(myfile.csv)
read myfirstfield,mysecondfield
loop


Any help would be much appreciated.

Thanks.











  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Still stuck with importing a delimitted file.

That did the trick.

Thanks a heap for your help.

On Thu, 8 Jan 2004 17:48:08 -0600, "RWN" wrote:

Tino;
Dimension your input fields as string.
By not dimensioning them basic assumes that the variables are
"Variants", which means it will decide what format they are.
Thus, when it sees the "1 0" it assumes that it is a numeric (it thinks
the blank is the sign byte).
Here's what I did, using your file.

Dim F1 As String
Dim F2 As String

Sub Testit()
Open "C:\xfer\tino.txt" For Input As #1
Do Until EOF(1)
Input #1, F1, F2
mysecondfield = (Int(Val(F2) * 100)) / 100 *** Told basic that I wanted
it to treat the string "F2" as a numeric value.

Debug.Print F1, F2, mysecondfield
Loop
Close #1

End Sub

And here's what I got

F1 ...............F2..............mysecondfield
abc 123 123.4 123.4
1 0 002 900.23231 900.23
1 0 003 31.12 31.12


--
Regards;
Rob
------------------------------------------------------------------------
wrote in message
...
Three example lines of my csv file are..

abc 123,123.4
1 0 002,900.23231
1 0 003,31.12

And it appears to recognise the spaces in the lines as delimeters as
well as the commas.

The purpose of my code is to open a text file and round values in the
second field of each line and the full code is as follows:

mycount = 1
Do While Range("a" & mycount) < ""

a = "c:\web\" & Range("a" & mycount) & ".csv"
b = "c:\web\" & Range("a" & mycount) & ".txt"
Open a For Input As #1
Open b For Output As #2

b = LCase(b)

Do Until EOF(1)
Input #1, myfirstfield, mysecondfield
mysecondfield = (Int(mysecondfield * 100)) / 100
Print #2, myfirstfield & "," & mysecondfield
Loop
Close
mycount = mycount + 1

Loop


Thanks again.

On Wed, 7 Jan 2004 23:04:25 -0600, "RWN" wrote:

Hmmm. Work fine here.
File;
123,456
789,012
"ROB,RWN","Test it"

Dim F1 as String
Dim F2 as String

Sub Testit()
Open "C:\xfer\test.csv" For Input As #1
Do Until EOF(1)
Input #1, F1, F2
Debug.Print F1, F2
Loop
Close #1
End Sub

Out
123 456
789 012
ROB,RWN Test it

Perhaps I'm missing something as to what your file looks like?

--
Regards;
Rob


-----------------------------------------------------------------------

-
wrote in message
...
Oops... It's not actually working quite right as it's not using

comma
as a delimeter. Some of the values in my first field have spaces

and
it appears to be picking up those as delimeters too.
'
Any ideas on how to resolve this?

Thanks

On Thu, 08 Jan 2004 03:34:58 GMT, )

wrote:

Thanks Rob!

On Wed, 7 Jan 2004 21:35:26 -0600, "RWN"

wrote:

Try this

Open "c:\myfile.csv" for Input as #1
Do until EOF(1)
Input #1,myfirstfield,mysecondfield
Loop

Close #1

--
Regards;
Rob


---------------------------------------------------------------------

-
--
wrote in message
...
Hi,

I do a lot of creating text files from Excel workbooks using

"Open
blahblah for output as #1" and then printing to that file with
"Print
#1, "etc"..

I now need to do some input from text files and don't know the
syntax
& can't find it in HELP.

Specifically, I have a comma separated text file with an

undefined
number of rows but always two fields.

What code would I need to achieve the following..

-Open a text file for reading
-Start a loop that will check line after line until the end of

the
file
-Take both fields of a line of data (comma separated) and put

each

field in a separate variable
-end the loop

I expect it would be something like this...(and I do know this
does
nothing at the moment. I will be doing further manipulation of

the
variables & dumping to another file later).

Open "c:\myfile.csv" for input as #1
Do while not EOF(myfile.csv)
read myfirstfield,mysecondfield
loop


Any help would be much appreciated.

Thanks.












Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
importing from a *.txt file Randy Bratton Excel Discussion (Misc queries) 4 October 15th 08 01:10 PM
file stuck in beta 2 yitsonlyme Excel Discussion (Misc queries) 1 April 25th 07 09:03 PM
Importing TXT file from web Murtaza Excel Discussion (Misc queries) 0 September 26th 06 03:51 AM
Importing text file, only option to edit existing file smokey99 Excel Discussion (Misc queries) 8 April 26th 06 09:08 PM
Help - now really stuck! File transfer problem ohboy! Excel Discussion (Misc queries) 10 May 2nd 05 09:07 PM


All times are GMT +1. The time now is 06:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"