+ Reply to Thread
Results 1 to 3 of 3

Consolidating VB code (Removing 29 different Special Characters)

Hybrid View

adgjqetuo Consolidating VB code... 06-10-2009, 11:29 PM
Paul Re: Consolidating VB code... 06-11-2009, 01:56 AM
adgjqetuo Re: Consolidating VB code... 06-11-2009, 07:25 AM
  1. #1
    Forum Contributor
    Join Date
    08-06-2007
    Posts
    105

    Consolidating VB code (Removing 29 different Special Characters)

    I made this VB code today - but problem is it's SO HUGE. Can someone possibly look at it and shorten it some?

    It basically just looks at a given range, and removes ALL special Characters - and then gives a break down as to how many it removed.

    This code DOES WORK AS IS - I just want to shorten it's length - not alter what it does.

    - Also, does anyone know of a better way to display the breakdown of characters removed? It's a little sloppy and the msgbox character limit maxed out.


    EDIT: I just tried to copy in the code and I maxed out this thead, haha. I attached a copy of the workbook.
    Attached Files Attached Files
    Last edited by adgjqetuo; 06-10-2009 at 11:32 PM.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Consolidating VB code (Removing 29 different Special Characters)

    Hi Adgjqetuo,

    The code to replace all of the special characters you've specified can be shortened to:
    Sub test()
    Dim myString As String, ce As Range, i As Long
    Application.ScreenUpdating = False
    For Each ce In Range("A1:V500")
        For i = Len(ce.Value) To 1 Step -1
            Select Case Mid(ce.Value, i, 1)
                Case Is = "`", "!", "@", "#", "$", "%", "^", "&", "(", ")", "_", "-", "=", "+", _
                    "{", "[", "}", "]", "\", "|", ";", ":", "'", """", ",", "<", ".", ">", "/"
                        myString = Replace(ce.Value, Mid(ce.Value, i, 1), "")
                        ce.Value = myString
                Case Else
                        ' Do Nothing
            End Select
        Next i
        myString = ""
    Next ce
    Application.ScreenUpdating = True
    End Sub
    This doesn't prompt the user whether they want to do the replacements, or show a results window with the count of each special character replaced. Is the latter really important? (Do the users care that 18 . and 4 / were removed?) If you absolutely need those features, they can be (somehow) added to this code.

    PS - Try this code on a backup copy of your workbook. Don't want you deleting something important.
    Last edited by Paul; 06-11-2009 at 02:04 AM. Reason: Added ScreenUpdating code to stop screen flicker. Code now takes under 2 seconds to run on A1:V500.

  3. #3
    Forum Contributor
    Join Date
    08-06-2007
    Posts
    105

    Re: Consolidating VB code (Removing 29 different Special Characters)

    This is great so far! Thank you!

    However, yes I do need those notifications as they try and keep record of what has been deleted at work. Usually we do a find for each character and writ down each time it does a replacement - because it tells you how many replacements have been found.

    But were off to a good start!

    Can someone help with the break-down part to added to this code? Perhaps a form maybe?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1