In this lesson, I’ll show you how to create a VBA macro to check whether the string contains a letter or a string.
Sub IfContains() If InStr(ActiveCell.Value, "string") > 0 Then MsgBox "The string contains the value." Else MsgBox "The string doesn't contain the value." End If End Sub
This simple procedure uses InStr function to return the first occurrence of a letter or a string in an active cell. In other words, if there is a substring inside, the number can’t be lower than 1, therefore if the InStr returns a value larger than 0, it means that a string or a character is inside, and the Sub displays a message box informing you about this.
If the cell is empty or it doesn’t contain the value it will display MsgBox informing you about this.
What you have to remember, is that the second argument in InStr function is case sensitive.