View Single Post
Old 07-20-2017, 04:34 AM
starmanMike starmanMike is offline
Junior Contributor
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 310
Default using split on txt file

Okay, just an addition.
I realised that I didn't need all this faffing about with schema.ini files and the rest - I could just use the split() function, which is what I did, and it works (sort of)...
Where the 'sort of' comes in is that, even though running through debug.print to check the output, the first line of the .txt file is included (which I don't want, it's the column headers) and I get an error saying 'subscript out of range'. The subscript isn't the array members of 'split' is it? since these go from 0 to 15 - and I only want some of them.
Here's the relevant coding:
Dim longstring As String
Dim bits() As String

Open afile For Input As #1
Do While Not EOF(1)
    Line Input #1, longstring
    bits = Split(longstring, "##")
        Debug.Print bits(1), bits(3), bits(5), bits(14)
        Close #1
End Sub
And here's a section from the debug.print:
StarName      	JD            		Mag           	ChartID 
 TZ CYG        	2457029.2     	11.0          		1995 
 S CYG         	2457029.2     	13.0          		4600QA 
 SV CEP        	2457029.2271             10.1       		2007 
 BO CEP        	2457029.2278             11.7          		7219ARX 
 SS CYG        	2457029.2306             12.1          		5167HDQ 
 CH CYG        	2457029.2333              7.2           		1995 
 YZ CEP        	2457029.2354             11.0          		1991 
 V1514 CYG     	2457029.241   	12.5          		4600QA 
(I assume the left-offset in the 'mag' & 'chartID' columns is just a result of there not being 4 d.p. in the 'JD' column).
The whole text file is echoed, so no problem there. (it's about 25 lines long and is just a test file. In reality the file could contain thousands of lines) but 2 questions:

1) Why am I getting a 'subscript out of range' error?
2) How do I prevent the unwanted top line being read? Could I maybe run a 'preamble' routine which deletes this line first?
Loads of thanks for any help.
Reply With Quote