VBA remove a word before Multiple Specific text

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, I am trying to delete the length of a word if multiple specific text is found in a cell, but the code I have deletes the whole string rather than just the length of the word which is variable, Can someone help.

Code:
    Dim c As range
    For Each c In Selection
        If InStr(c.value, " Pie") > 0 Then
            c.value = Left(c.value, InStr(c.value, " Pie") - 1)
        End If
            If InStr(c.value, "Pie") > 0 Then
         c.value = Left(c.value, InStr(c.value, "Pie") - 1)
        End If
        If InStr(c.value, " Split") > 0 Then
            c.value = Left(c.value, InStr(c.value, " Split") - 1)
        End If
        If InStr(c.value, "Split") > 0 Then
         c.value = Left(c.value, InStr(c.value, "Split") - 1)
        End If
    Next c
End Sub

Example:

The multiple specific text I am trying to find is Pie and Split

[TABLE="class: grid, width: 300"]
<tbody>[TR]
[TD]Something MyApple Pie Something[/TD]
[/TR]
[TR]
[TD]Something YourApple Pie Something[/TD]
[/TR]
[TR]
[TD]Something TheirApplePie Something[/TD]
[/TR]
[TR]
[TD]Something Banana Split Something[/TD]
[/TR]
[TR]
[TD]Something BananasSplit Something[/TD]
[/TR]
</tbody>[/TABLE]


Then Delete the Length of the word before the text found

[TABLE="class: grid, width: 200"]
<tbody>[TR]
[TD]Something Something[/TD]
[/TR]
[TR]
[TD]Something Something[/TD]
[/TR]
[TR]
[TD]Something Something[/TD]
[/TR]
[TR]
[TD]Something Something[/TD]
[/TR]
[TR]
[TD]Something Something[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Hi Fennek, I know of RegEx: but no idea where to start with this.
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Running both codes returned the correct answer.
I've altered the code in line below for just numbers ending in "p"
Code:
If IsNumeric(Left(sp(n), Len(sp(n)) - 1)) And Right(sp(n), 1) = "p" Then
You could try replacing that data in the file I send , alter the "p" bit, see what happens ??
 
Last edited:
Upvote 0
Hi,

at the moment I can't test it, so the code is from my archive (a bit adopted)

Code:
Function catchNumeric(TextValue As String) As string
    Dim regex As Object
    Set regex = CreateObject("VBscript.regexp")
    With regex
        .Pattern = "\s\d+p\s"
        If .test(TextValue) Then
        catchNumeric = .Replace(TextValue, "")
    End If
    End With
End Function

regards
 
Upvote 0
Fennek, your code worked for me as below:-

Code:
[COLOR="Navy"]Sub[/COLOR] MG12Dec03
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] sp [COLOR="Navy"]As[/COLOR] Variant, nStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] nSp [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
  [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]Dim[/COLOR] objRegex
    [COLOR="Navy"]Set[/COLOR] objRegex = CreateObject("vbscript.regexp")
    [COLOR="Navy"]With[/COLOR] objRegex
      .Global = True
      .Pattern = "\s\d+p\s"
      Dn.Value = .Replace(Dn.Value, " ")
    [COLOR="Navy"]End[/COLOR] With
 [COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi MickG, Changing that one line has worked perfectly. Hi Fenneck, Your RegEx code works perfectly aswell, Thank you both for taking time to help me with this. It's much appreciated
 
Upvote 0

Forum statistics

Threads
1,223,754
Messages
6,174,311
Members
452,554
Latest member
Louis1225

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