How to remove ALL numbers from a cell with a function or regex?

前端 未结 3 1649
逝去的感伤
逝去的感伤 2021-02-10 17:30

I have cells that look like this, one per line:

Duffy,John: \'Heritage: Civilization and the Jews\'- Fanfare & Chorale,Symphonic Dances + Orchestr

相关标签:
3条回答
  • 2021-02-10 17:48

    UPDATED: I had accidentally posted a contains function instead of replace!

    Search and replace with Regex works fine, but you might want to edit your text afterwords (like commas that were there before the numbers, etc.).

    Here is the function. Pattern is simply =RegexReplace(cell, "\d", "")

    Function RegexReplace(ByVal text As String, _
                          ByVal replace_what As String, _
                          ByVal replace_with As String) As String
    
    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    
    RE.Pattern = replace_what
    RE.Global = True
    RegexReplace = RE.Replace(text, replace_with)
    
    End Function
    
    0 讨论(0)
  • 2021-02-10 17:54

    How to do that using VBA...

    1. Open an Excel workbook and paste the text you provided this way: alt text

    2. Let those rows selected.

    3. Press “ALT+F11” to open the Visual Basic Editor.

    4. Go to the Insert Menu and open a Module.

    5. Type in this function:

      Sub clear()
      s = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
      For Each r In Selection
      v = r.Value
      For i = 0 To 9
      v = Replace(v, s(i), "")
      Next
      r.Value = v
      Next
      End Sub
      

    6. Click the green Play button to execute the VBA script. alt text

    7. Get back to the sheet and see the result (no more digits): alt text

    0 讨论(0)
  • 2021-02-10 18:13

    I was faced with a similar problem, but went another way, wanting to avoid using vba. A recursive use of substitute did the trick for me, as illustrated below:

    Original data (in A1:An, n=integer) ROE -1.00 P/E 0.07 -0.85 ROC-ROE 0.02 -0.03 etc..

    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"1",""),"0",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9","")

    which can be extended to get rid of "-" and the decimal indicator "." as well as "+" (if relevant) and finally, superfluous spaces using

    =trim(substitute(substitute(substitue(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"1",""),"0",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9",""),"-",""),"+","")," .",""))

    where the lowercase represents complications added in the current step.

    This maps naturally onto the entire array by changing the range reference to the entire array to be parsed (A1:An) and entering the formula as an array (i.e., using Ctl+Enter instead of Enter to complete the task).

    I then use len(B1) to check the results are what I want.

    Not elegant, admittedly. But may be a useful exercise for teaching students to think and code "on the fly."

    0 讨论(0)
提交回复
热议问题