Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a visual basic macro in Excel
I am looking for the strings I'm I'll I'd
Coding as above using "I'" bypasses the text

What I have tried:

Searching for I'm separately does not work either
Using ""I'm"" gives a compilation error
Any workarounds greatly received for 67 year old coder with 50 years IT experience.
Darrell
Posted
Updated 26-Feb-19 20:31pm
Comments
Patrice T 26-Feb-19 18:03pm    
A coder with 50 years of experience should know that showing its code is the first thing to do wheb requesting help.
Member 14163890 26-Feb-19 18:09pm    
YJanks for your prompt reply. The code shows in the header on my session

If InStr(MyMessage, "I'") <> 0 Then

I have written a visual basic macro in Excel
I am looking for the strings I'm I'll I'd
Coding as above using "I'" bypasses the text

Definitely not a compiler bug. VBA has been around for decades so that possibility is extremely remote and you're not doing anything "on the edge".

This works as expected for me:
VB.NET
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim message

    message = "I'm OK"
    
    If InStr(message, "I'") <> 0 Then
        MsgBox "Found"
    Else
        MsgBox "Not Found"
    End If
End Sub

Have you used "Option Compare" at the top of your code?
 
Share this answer
 
v2
Well... Replace double quotation marks with single, so ""I'll"" should be replaced with "I'll" and everything will be fine. See:

VB
Option Explicit


Sub test()
    Dim Strings2Find As Variant
    Dim Text2SearchIn As String
    Dim i As Integer, j As Integer
    
    Text2SearchIn = "I'm glad i can see you. I'd like to introduce some one to you. I'll be very happy, if you'll..." 'text is not important ;)
    Strings2Find = Array("I'm", "I'll", "I'd")
    
    
    For i = LBound(Strings2Find) To UBound(Strings2Find)
        Do
            j = InStr(j + 1, Text2SearchIn, Strings2Find(i))
            If j > 0 Then MsgBox "'" & Strings2Find(i) & "' has been found on position: " & j & vbCr & _
            "'" & Text2SearchIn & "'", vbOKOnly, "Success!"
        Loop While j > 0
    Next


End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900