1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Excel Question

Discussion in 'BBS Hangout' started by Haymitch, Oct 31, 2018.

  1. Haymitch

    Haymitch Custom Title

    Joined:
    Dec 22, 2005
    Messages:
    28,371
    Likes Received:
    24,021
    I want to delete all non-alphanumeric characters in columns H & I, except for spaces.

    So if the cell says "%$187 Square" I want it to end up saying "187 Square"

    I have a macro that deletes the special characters AND the space, resulting in "187Square" but I need that space to not be deleted.

    Please advise

    Code:
    Sub RemoveNonANCharacters()
      Dim x As Long, Text As String, Cell As Range, Rng As Range
      Set Rng = Range("H:I")
      For Each Cell In Rng
        Text = Cell.Value
        For x = 1 To Len(Text)
          If Mid(Text, x, 1) Like "[!A-Za-z0-9]" Then Mid(Text, x) = Chr(1)
        Next
        Cell = Replace(Text, Chr(1), "")
      Next
    End Sub
     
  2. DudeWah

    DudeWah Member

    Joined:
    Oct 10, 2007
    Messages:
    9,643
    Likes Received:
    3,523
    @el gnomo your love of excel could come in handy here

    @kevC Harold plz

    @mrm32 oil and gas skills help
     
  3. HR Dept

    HR Dept Member

    Joined:
    May 31, 2012
    Messages:
    6,792
    Likes Received:
    1,223
    The only solution is to concatenate your V-Lookup and utilize SQL. Unfortunately, there is no other way.
     
  4. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,612
    Likes Received:
    33,588
    Can't you just change the line with the LIKE to :

    If Mid(Text, x, 1) Like "[!A-Za-z0-9]" And Mid(Text, x, 1) <> " " Then Mid(Text, x) = Chr(1)
     
  5. DudeWah

    DudeWah Member

    Joined:
    Oct 10, 2007
    Messages:
    9,643
    Likes Received:
    3,523
    Is that English?
     
  6. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,612
    Likes Received:
    33,588
    It sure ain't Python!
     
  7. DudeWah

    DudeWah Member

    Joined:
    Oct 10, 2007
    Messages:
    9,643
    Likes Received:
    3,523
    I’ll put a Carolina reaper in your chili. Excel that.
     
    B-Bob likes this.
  8. Haymitch

    Haymitch Custom Title

    Joined:
    Dec 22, 2005
    Messages:
    28,371
    Likes Received:
    24,021
    Nah that didn't work
     
  9. heypartner

    heypartner Member

    Joined:
    Oct 27, 1999
    Messages:
    63,510
    Likes Received:
    59,001
    Did you put a space between the quotes? Try "[ ]" too.

    However, to avoid an extra Mid call, try putting the space in your original LIKE statement

    like this*:

    If Mid(Text, x, 1) Like "[!A-Za-z0-9 ]" Then Mid(Text, x) = Chr(1)

    *note the space after the 9. I think the VBA Like format matches that way.

    fwiw: that For Loop looks horribly inefficient, if you have large amounts of cells to change. Have you considered just using the Regex.Replace feature of VBA, instead? That would replace the *entire* FOR LOOP and the line after the NEXT. You might have reasons to not use that. But if so, that's going to be faster than calling Mid *TWICE* for every character of every cell.

    Let us know, and we can share how to do that without a For Loop.
     
    #9 heypartner, Nov 1, 2018
    Last edited: Nov 1, 2018
  10. Air Langhi

    Air Langhi Contributing Member

    Joined:
    Aug 26, 2000
    Messages:
    21,935
    Likes Received:
    6,685
    You should use regex. Are the non alpha character only at the start or end? In that case do ^(.*)[A-Za-z0-9] or [A-Za-z0-9](.*)$ and then delete the matching string using a replace.
     
  11. Haymitch

    Haymitch Custom Title

    Joined:
    Dec 22, 2005
    Messages:
    28,371
    Likes Received:
    24,021
    Yeah putting that space in there did the trick. It dawned on me after posting this morning.

    But, as you said, it's very inefficient. Takes way too long to run.

    I did consider the regex route but I have never used it before. Not sure how to get it to work.

    If someone could help that would be great!!
     
  12. heypartner

    heypartner Member

    Joined:
    Oct 27, 1999
    Messages:
    63,510
    Likes Received:
    59,001
    Here's your code using Regex instead. Notice no inner FOR LOOP. the RegEx.Replace call operates on all matches in the string at once....if global=true is on.

    Code:
    Sub RemoveNonANCharacters()
      Dim x As Long, Text As String, Cell As Range, Rng As Range
      Dim RegEx As Object
      Set RegEx = CreateObject("VBScript.RegExp")
      RegEx.Global = True
      RegEx.Pattern = "[^A-Za-z0-9 ]"
    
      Set Rng = Range("H:I")
    
      For Each Cell In Rng
         Cell = RegEx.Replace(Cell.Value, "")
      Next
    
    End Sub
    note: you usually have to "turn on" Regular Expression for your code. See this for how to enable the RegExp Object.

    https://stackoverflow.com/questions...ct-regular-expression-using-vba-macro-in-word

    note note: the pattern will use a carat ^ rather than ! for "not" such and such char. see my code.
     
    #12 heypartner, Nov 1, 2018
    Last edited: Nov 1, 2018
    Haymitch likes this.
  13. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,612
    Likes Received:
    33,588
    Odd. Works when I try it on your example string in my Excel. Make sure there's a space in the quotes. Oh well, I agree with the others, though -- on a large spreadsheet, that thing's going to take forever to churn. If you're just doing a few dozen rows, it's not a huge deal, but if you're doing hundreds or thousands of rows, it is. I accidentally ran it on an entire spreadsheet last night and, dang, that was a mistake. :D
     
    DudeWah likes this.
  14. B-Bob

    B-Bob "94-year-old self-described dreamer"
    Supporting Member

    Joined:
    Jul 26, 2002
    Messages:
    35,975
    Likes Received:
    36,809
    Better or worse than a poop thread?
     
    DudeWah likes this.
  15. heypartner

    heypartner Member

    Joined:
    Oct 27, 1999
    Messages:
    63,510
    Likes Received:
    59,001
    I'm still waiting for What to come in and say how the best way is for everyone to stop using Excel, install Linux with dual boot, then use Gnumeric..."It's much easier."
     
    Haymitch, B-Bob and arkoe like this.
  16. ipaman

    ipaman Member

    Joined:
    Nov 23, 2002
    Messages:
    13,201
    Likes Received:
    8,040
    Substitute all spaces with a keyword not found in the list, for example "clutchfans" first. Run you macro. Then replace all "clutchfans" with a " "
     
  17. DudeWah

    DudeWah Member

    Joined:
    Oct 10, 2007
    Messages:
    9,643
    Likes Received:
    3,523
    No he would tell you to use LibreOffice Calc.

    Lol
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now