View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
GS[_6_] GS[_6_] is offline
external usenet poster
 
Posts: 1,182
Default Tricky regular expression

You could simply split the lines using the space as the delimiter; - skip the
empty element! Your sample text "a123 A. BC #123", for example:

vTmp = Split(sLine, " ") returns the following...
vTmp(0) = "a123"
vTmp(1) = ""
vTmp(2) = "A."
vTmp(3) = "BC"
vTmp(4) = "#123"

...so checking each line for content is the lines to process.

HOWEVER:

You should read the entire file into an array and process (dump) it into a
blank worksheet, then parse the data however desired. Alternatively, you could
process each line with the code I posted earlier and do whatever with the
results.

Here's how:

Sub ParseFile()
Dim sFile$, sTextIn$, vData, n&, rng

'Select the file
sFile = Get_FileToOpen(): If sFile = "" Then Exit Sub

'Get file contents into an array
sTextIn = ReadTextFile(sFile): vData = Split(sTextIn, vbCrLf)

'Dump the array into a blank worksheet
ActiveWorkbook.Sheets.Add
Set rng = ActiveSheet.Cells(1).Resize(UBound(vData) + 1)
rng = Application.Transpose(vData)
'Process each cell's contents
For n = 1 to rng.rows.count
If StrChk(rng.Cells(n)) Then
'do something...
Else
'do something else...
End If
Next 'n

'-OR- Process each element of the array
For n = LBound(vData) to UBound(vData)
If StrChk(vData(n)) Then
'do something...
Else
'do something else...
End If
Next 'n
End Sub 'ParseFile

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one shot.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

Function Get_FileToOpen$(Optional FileTypes$)
Dim vFile
If FileTypes = "" Then FileTypes = "All Files ""*.*"", *.*"
vFile = Application.GetOpenFileName(FileTypes)
Get_FileToOpen = IIf(vFile = False, "", vFile)
End Function

Function StrChk(sText$) As Boolean
Select Case Asc(Left(sText, 1))
Case 97 To 122
Select Case Asc(Right(sText, 1))
Case 48 To 122: StrChk = (InStr(sText, " ") 0)
End Select '//Asc(Right(sText, 1))
End Select '//Asc(Left(sText, 1))
End Function '//StrChk()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion