View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Claus Busch Claus Busch is offline
external usenet poster
 
Posts: 3,872
Default Tricky regular expression

Hi Robert,

Am Sun, 19 May 2019 11:06:28 -0700 (PDT) schrieb RG III:

Well, I'm adding functionality to an existing spreadsheet, and I prefer NOT to dump the file contents into an empty sheet. I wanted my code to be as compact, fast, and non-intrusive as possible.

The code that I posted earlier should have worked, and it fits my needs, but for some reason the OpenTextFile and ReadLine combination somehow taints the file string and prevent's Claus's solution from working.


try:

Function StrChk(myStr As String) As Boolean
Select Case Asc(Left(myStr, 1))
Case 97 To 122
Select Case Asc(Right(myStr, 1))
Case 48 To 122
If InStr(myStr, " ") 0 Then StrChk = True
End Select
End Select
End Function

Sub ParseFile()
Dim n As Integer, i As Integer, RowNmbr As Integer
Dim varOut() As Variant
Dim sText As String

Const sPath = "G:\Data\data.txt"

'Select the file
Open sPath For Input As #1

Do Until EOF(1)
RowNmbr = RowNmbr + 1
Line Input #1, sText
If StrChk(sText) = True Then
ReDim Preserve varOut(i)
varOut(i) = RowNmbr
i = i + 1
End If
Loop
Close #1
MsgBox "The rows:" & Chr(10) & Join(varOut, Chr(10)) & _
Chr(10) & "of the text file are correct"
End Sub

The messagebox will show you the row numbers with correct data.


Regards
Claus B.
--
Windows10
Office 2016