How to identify a set of text and a string of random text behind it.

NewVbaJer

New Member
Joined
Jul 6, 2021
Messages
2
Dear all,

I am trying to write a VBA code that first identifies a string of text that has no text behind it. Then, it would add some text once the two conditions are fulfilled.

For example, I am trying to replace "P - " with "P - None". However, it should not become "P- Nonexxxxxxxx" if there are text behind.
1625561391103.png


I have only managed to write the first set of condition but I am struggling with the second, that won't interfere if there some text behind.

VBA Code:
Dim i As Long
For i = 5 To Cells(Rows.Count, "F").End(xlUp).Row

If InStr(Cells(i, "F").Value, "P -") > 0  Then
Cells(i, "F").Replace What:="P -", Replacement:="P - None"

Any guidance or help is greatly appreciated!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Providing it's "P -" and not "P - " this should prevent "P - xxxx" from being included:
If (InStr(Cells(i, "F").Value, "P -")) > 0 And Len(Cells(i, "F").Value) = 3 Then
 
Upvote 0
If you have multiple lines per cell, maybe
VBA Code:
If InStr(Cells(i, "F").Value & vbLf, "P -" & vbLf) > 0 Then
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,846
Members
449,194
Latest member
HellScout

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