Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Extracting data with delimiters

I would like to extract data from a text file delimited by semi colons.
The text file looks like:
XXX : XXX : XXX
XXX : XXX : XXX
XXX : XXX : XXX
XXX : XXX : XXX

So far I would just like to take the only the first line and put i
into a 3 element array. Although inefficient, I created three separat
loops for each column for clarity and since the last column may contai
colons. This is what I have so far, and it seems to fail in the firs
loop and I don't know why. Any help would be appreciated. Attached i
the text file I am using.

Sub grabSTA()
' Gets filename from cell E1 as uses it as input #1
Dim strFILENAME As String
Dim textColonArray(3) As String
strFILENAME = Sheet1.Range("E1").Value
Open strFILENAME For Input As #1

Dim currChar, tmpTxt As String

currChar = Input(1, #1)

' First Block
tmpTxt = ""
Do
tmpTxt = tmpTxt & currChar

currChar = Input(1, #1)
Loop While (currChar < ":")
textColonArray(0) = tmpTxt
Sheet1.Range("E3").Value = Trim(textColonArray(0))

' Second Block
tmpTxt = ""
Do
tmpTxt = tmpTxt & currChar
currChar = Input(1, #1)
Loop While (currChar < ":")
textColonArray(1) = tmpTxt
Sheet1.Range("E4").Value = Trim(textColonArray(1))

' Third Block
tmpTxt = ""
Do
tmpTxt = tmpTxt & currChar
currChar = Input(1, #1)
Loop While ((Asc(currChar) < 10) Or (Asc(currChar) < 13) O
(Not EOF(1)))
textColonArray(2) = tmpTxt
'Sheet1.Range("E5").Value = Trim(textColonArray(2))

Close #1

End Sub


:

Attachment filename: short_sch_zny.txt
Download attachment: http://www.excelforum.com/attachment.php?postid=46446
--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default Extracting data with delimiters

Tippy,

The approach is indeed inefficient, so instead of bothering to find the
error, I took a different one from scratch. Basically what it does is it
reads a whole line and locates the separators, then breaks it up based on
the separator positions:

Sub grabSTA()
Dim strFILENAME As String
Dim textColonArray(2) As String

Sheet1.Range("E3").Select
strFILENAME = Sheet1.Range("E1").Value
Open strFILENAME For Input As #1
Line Input #1, lin
sep1 = InStr(1, lin, ":")
sep2 = InStr(sep1 + 1, lin, ":")
textColonArray(0) = Trim(Left(lin, sep1 - 1))
textColonArray(1) = Trim(Mid(lin, sep1 + 1, sep2 - sep1 - 1))
textColonArray(2) = Trim(Right(lin, Len(lin) - sep2))
For i = 0 To 2
ActiveCell.Offset(i, 0) = textColonArray(i)
Next
Close #1
End Sub

This approach can easily be modified to import all data lines (rejecting the
dashes), and put in three columns, much like the original file:

Sub garbSTA_All_Lines()
Dim strFILENAME As String
Dim textColonArray(2) As String

Sheet1.Range("E3").Select
strFILENAME = Sheet1.Range("E1").Value
Open strFILENAME For Input As #1
Sheet1.Range("E3").Select
linecount = 0
Do Until EOF(1)
Line Input #1, lin
If Left(lin, 3) < "---" Then
Debug.Print lin
sep1 = InStr(1, lin, ":")
sep2 = InStr(sep1 + 1, lin, ":")
textColonArray(0) = Trim(Left(lin, sep1 - 1))
textColonArray(1) = Trim(Mid(lin, sep1 + 1, sep2 - sep1 - 1))
textColonArray(2) = Trim(Right(lin, Len(lin) - sep2))
For i = 0 To 2
ActiveCell.Offset(linecount, i) = textColonArray(i)
Next
linecount = linecount + 1
End If
Loop
Close #1
End Sub

Note: with the separator locator inside the loop, it can cope with separator
positions varying from line to line. If you are certain they are constant,
this part can be pulled before the loop and thus executed only once,
marginally improving execution performance.

HTH,
Nikos

"Tippy " wrote in message
...
I would like to extract data from a text file delimited by semi colons.
The text file looks like:
XXX : XXX : XXX
XXX : XXX : XXX
XXX : XXX : XXX
XXX : XXX : XXX

So far I would just like to take the only the first line and put it
into a 3 element array. Although inefficient, I created three separate
loops for each column for clarity and since the last column may contain
colons. This is what I have so far, and it seems to fail in the first
loop and I don't know why. Any help would be appreciated. Attached is
the text file I am using.

Sub grabSTA()
' Gets filename from cell E1 as uses it as input #1
Dim strFILENAME As String
Dim textColonArray(3) As String
strFILENAME = Sheet1.Range("E1").Value
Open strFILENAME For Input As #1

Dim currChar, tmpTxt As String

currChar = Input(1, #1)

' First Block
tmpTxt = ""
Do
tmpTxt = tmpTxt & currChar

currChar = Input(1, #1)
Loop While (currChar < ":")
textColonArray(0) = tmpTxt
Sheet1.Range("E3").Value = Trim(textColonArray(0))

' Second Block
tmpTxt = ""
Do
tmpTxt = tmpTxt & currChar
currChar = Input(1, #1)
Loop While (currChar < ":")
textColonArray(1) = tmpTxt
Sheet1.Range("E4").Value = Trim(textColonArray(1))

' Third Block
tmpTxt = ""
Do
tmpTxt = tmpTxt & currChar
currChar = Input(1, #1)
Loop While ((Asc(currChar) < 10) Or (Asc(currChar) < 13) Or
(Not EOF(1)))
textColonArray(2) = tmpTxt
'Sheet1.Range("E5").Value = Trim(textColonArray(2))

Close #1

End Sub


:)

Attachment filename: short_sch_zny.txt
Download attachment:

http://www.excelforum.com/attachment.php?postid=464469
---
Message posted from http://www.ExcelForum.com/



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
Multiple Delimiters Doug Mc New Users to Excel 9 October 18th 09 03:36 PM
Multiple Delimiters hassanq23 Excel Discussion (Misc queries) 5 April 7th 09 01:16 AM
delimiters and manipulation PBArich Excel Worksheet Functions 0 June 12th 07 06:46 PM
Missing delimiters after 15 records BrianFromMN Excel Discussion (Misc queries) 3 September 1st 06 10:58 PM
Parse data with uneven lengths and different delimiters [email protected] Excel Worksheet Functions 3 January 5th 06 11:57 PM


All times are GMT +1. The time now is 10:54 AM.

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

About Us

"It's about Microsoft Excel"