Visual Basic > Applications-VBA
Given a text file, it is required to print only lines that c
Given a text file, it is required to print only lines that c Test file is: strTest = "I am going to the market. I am" _ & " going to the cinema. I am going home. I am mysorian." This should print only three lines ----------- 'The code follows: 'Button click calls howMany '---------- Private Sub Command1_Click() Call howMany End Sub '-----------procedure howMany------- Sub howMany() 'This procedue takes the string and counts the number of occurences of period(.) 'variable occur stores the number of occurences strTest = "I am going to the market. I am" _ & " going to the cinema. I am going home. I am mysorian." Dim leng leng = Len(strTest) Debug.Print leng Dim cnt As Integer cnt = 1 Dim occur As Integer occur = 0 Dim i Do intpos = InStr(cnt, strTest, ".") Debug.Print intpos cnt = intpos + 1 If intpos = 0 Then Exit Do End If occur = occur + 1 Loop Debug.Print occur 'the procedure token takes the 'occur' variable to chop the text file Call token(occur) End Sub '--------procedure token------ Sub token(ByVal ocr As Integer) 'takes a string and using the delimiter, the period symbol(.) 'tokenizes, that is it splits into individual sentences strTest = "I am going to the market. I am" _ & " going to the cinema. I am going home. I am mysorian." Dim strg As Variant strg = Split(strTest, ".", -1) 'long -1 gets all the lines Dim i For i = 0 To ocr 'you should pass the number of occurences of period to get all the sentences 'Debug.Print strg(i) & vbCrLf 'procedure [going] determiens whether or not to print Call going(strg(i)) Next i End Sub '------procedure going---------- Sub going(ByVal strYes As String) strF = "going" Dim inpos inpos = InStr(strYes, strF) If inpos > 1 Then Debug.Print strYes Else End If End Sub '--------------- Result obtained as follows: 85 25 52 70 85 0 4 I am going to the market I am going to the cinema I am going home
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows