0

I want to search a multi line string in a text file using VBA excel macro. I tried using InStr function.But its not working as I expected. My exact aim is to read a multi line string stored in a cell and check whether it is available in a text file. For that what i did is read the text file in to a variable, reading the string saved in the cell to another variable and comparing using Instr using binary comparison. Will InStr work for multi line string? If not any any other way to compare it?

This is my code

Public Function string_compare() As String
    Dim strFilename As String
    Dim strSearch As String
    strFilename = "D:\test.txt"
    Dim strFileContent As String
    Dim iFile As Integer: iFile = FreeFile
    Open strFilename For Input As #iFile
    strFileContent = Input(LOF(iFile), iFile)
    Close #iFile
    strSearch = Sheet1.Cells(9, 1).Value
    If InStr(1, strFileContent, strSearch, vbBinaryCompare) > 0 Then
        MsgBox "success"
    Else
        MsgBox "failed"
    End If
End Function

When I checked the strings both seems to be identical.Even though the strings are identical, the searching result always failing. Any suggestions will be helpful.

6
  • Have you tried concatenating the lines together? Commented Jul 2, 2016 at 6:16
  • 2
    A multi-line string in an Excel cell will typically have vbLf (ASCII 10) as a line separator. A text file (on Windows) will typically be vbCrLf (ASCII 13 + ASCII 10). These will not match if you use Instr(), so you could try replacing the vbLf in the text from the cell with vbCrLf before using Instr() Commented Jul 2, 2016 at 6:40
  • As suggested by @TimWilliams, before using InStr function, write: strFileContent = Application.WorksheetFunction.Substitute(strFileContent, vbCrLf, "") and strSearch = Application.WorksheetFunction.Substitute(strSearch, vbLf, "") Commented Jul 2, 2016 at 6:43
  • VBA has a Replace() function since Excel 2000. If the line-breaks are significant then you should normalize them to the same version: if they're not significant then you can just remove them altogether. Commented Jul 2, 2016 at 6:47
  • Thank you very much Tim and Mrig. Its working fine as expected.How to make it answered?Hope it will help somebody else too. Commented Jul 2, 2016 at 15:17

1 Answer 1

0

As Tim and Mrig suggested I removed the cr and crlf from the text as follows. Now its working fine.I could use this for comparing multi line strings.I am posting my code segment here.Hope it may help somebody else too.

Public Function stringcompare(sourcefile As String, Workbookname As Worksheet) As String
Dim strSearch As String
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open sourcefile For Input As #iFile
strFileContent = Input(LOF(iFile), iFile)
Close #iFile
strSearch = Workbookname.Cells(1, 1).Value
strFileContent = Application.WorksheetFunction.Substitute(strFileContent, vbCrLf, "")
strSearch = Application.WorksheetFunction.Substitute(strSearch, vbLf, "")
    If StrComp(strFileContent, strSearch, vbBinaryCompare) = 0 Then
        MsgBox "success"
    Else
        MsgBox "failed"
    End If
End Function
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.