Find specific text in string

bobkap

Active Member
Joined
Nov 22, 2009
Messages
313
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I am trying to find if a string of text contains a specific word and if it does, to enter a word in a cell to the right of the text. Here's my code. For some reason the instring number always comes out to 39. But in this case, for every row that has the word "eCard" in the 5th column I want to show the word "eCard" or something like that. With this code EVERY row I have shows "eCard" even if the word in not present.
VBA Code:
Sub Find_Text_In_String()
'
' Find_Text_In_String Macro
'
Dim bcol As Integer
Dim finalcol As Integer
Dim finalrow As Integer
Dim banana As Variant
bcol = Application.Match("Memo", Range("1:1"), 0)

finalcol = Cells(1, Columns.Count).End(xlToLeft).Column
finalrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For counter = finalrow To 2 Step -1
If InStr(Cells(finalrow, bcol).Value, "eCard") > 0 Then
Cells(counter, bcol + 4) = "eCard"
End If
Next counter

'
End Sub
 
Last edited by a moderator:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Shouldn't this row
VBA Code:
If InStr(Cells(finalrow, bcol).Value, "eCard") Then

Be

VBA Code:
If InStr(Cells(COUNTER, bcol).Value, "eCard") Then
And make sure the syntax of "eCard" is exactly that
not, for instnace...."ecard"
 
Upvote 0
Shouldn't this row
VBA Code:
If InStr(Cells(finalrow, bcol).Value, "eCard") Then

Be

VBA Code:
If InStr(Cells(COUNTER, bcol).Value, "eCard") Then
And make sure the syntax of "eCard" is exactly that
not, for instnace...."ecard"
GRRRRR.....thanks!! I really appreciate it, but I'm embarrassed that I didn't see that. :)
 
Upvote 0
.. trying to find if a string of text contains a specific word and if it does...
It mat not be an issue with your data but it would mark "eCard" on a row that contained "TeleCard" even though the word "eCard" does not appear in that row. Is that likely to be an issue for you?
 
Upvote 0

Forum statistics

Threads
1,214,858
Messages
6,121,956
Members
449,057
Latest member
FreeCricketId

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top