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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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