Find specific text in string

bobkap

Board Regular
Joined
Nov 22, 2009
Messages
241
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:

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,052
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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"
 

bobkap

Board Regular
Joined
Nov 22, 2009
Messages
241
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
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. :)
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
48,400
Office Version
  1. 365
Platform
  1. Windows
.. 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?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,861
Messages
5,627,307
Members
416,239
Latest member
Counselor85027

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
Top