2

In my Excel workbook, there are a lot of occurrences of 00XX, where X can be any digit. For instance, 0017 appears frequently. I want to remove all occurrences of 00XX using the SUBSTITUTE function. So far, I have this:

=SUBSTITUTE(C1,"00[0-9][0-9]","")

This is not working though, and I think it is because Excel treats the second string as a literal, instead of a regular expression. Is there a way to make this formula work? I know that some programming languages allow you to add modifiers to a string to change its meaning, but I have not found any such thing yet for Excel.

Community
  • 1
  • 1
Lee White
  • 3,649
  • 8
  • 37
  • 62

3 Answers3

0

This solution uses no VBA

Pick an unused column, say column K.

In K1 enter:

="00" & TEXT(ROW()-1,"00")

copy this down from K2 thru K100. This is a list of all possible "undesirable" sub-strings.

With raw data in A1, in B1 enter the array formula:

=IFERROR(TRIM(SUBSTITUTE(A1,INDEX(K1:K100,MATCH(1,COUNTIF(A1,"*"&K1:K100&"*"),0)),"")),A1)

Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.

If A1 contains:

t0078yetryet

B1 would display:

tyetryet

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
0

Why not simply take advantage of the fact that the SEARCH function accepts wildcards?

=REPLACE(A1,LOOKUP(2^15,FIND(TEXT(ROW(INDEX(A:A,1):INDEX(A:A,99)),"0000"),A1)),4‌​,"")

Note that neither of the two solutions so far proposed will replace more than one occurrence of such substrings simultaneously on a given string.

Regards

brettdj
  • 54,857
  • 16
  • 114
  • 177
XOR LX
  • 7,632
  • 1
  • 16
  • 15
0
  1. Use the MoreFunc addin (web link seems to be downe
  2. VBA UDF below, use such as =CleanString(A1)

test

Sub TestDaCode()
MsgBox CleanString("0018 2450 00notme 0017")    
End Sub

udf

Function CleanString(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Global = True
    .Pattern = "00\d{2}"
    CleanString = .Replace(strIn, vbNullString)
End With
End Function
brettdj
  • 54,857
  • 16
  • 114
  • 177