Consulting

Results 1 to 3 of 3

Thread: Removing Punctuation Macro Also Removing Leading Zeroes

  1. #1
    VBAX Regular
    Joined
    Jun 2012
    Posts
    20
    Location

    Removing Punctuation Macro Also Removing Leading Zeroes

    I'm running the following macro to remove unwanted characters, however it's also currently stripping out leading zeroes from any columns that have them. Is there a way around this?

    [VBA]Sub RemovePunctuation()

    Worksheets("Data").Activate

    With CreateObject("vbscript.regexp")

    .Pattern = "[^a-zA-Z^0-9\ ]"

    .Global = True

    For Each Rng In Selection.SpecialCells(xlCellTypeConstants)

    Rng.Value = .Replace(Rng.Value, vbNullString)

    Next Rng

    End With

    End Sub

    [/VBA]

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Might try ..


    [VBA]

    Rng.NumberFormat = "@"
    Rng.Value = .Replace(Rng.Value, vbNullString)

    [/VBA]


    Paul

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [VBA]Sub RemovePunctuation()
    Dim rng As Range

    Worksheets("Data").Activate

    With CreateObject("VBScript.Regexp")

    .Pattern = "[^a-zA-Z^0-9\ ]"
    .Global = True

    For Each rng In Selection.SpecialCells(xlCellTypeConstants)

    rng.Value = "'" & .Replace(rng.Text, vbNullString)
    Next rng
    End With
    End Sub
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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