Modify Code to Remove Punctuation

CT Witter

MrExcel MVP
Joined
Jul 7, 2002
Messages
1,212
I have the following code to remove punctuation. Wanted to see if anyone had suggestions on how to make smaller/more efficient by removing if/else statements.

Thanks,
CT

Code:
Public Function NoPunc(varPhrase As Variant, strFlag As String) As String
    On Error GoTo Err_NoPunc
    '************************************************************************************
    ' Valid values for strFlag:
    ' C -   Parenthesis ( ) { } [ ]
    ' M -   Misc        " @ # $ % ^ & | ' ` ~ _
    ' O -   Operators   - + = * / \
    ' P -   Puncuation  , . ? ; :
    ' A -   All of the above
    '
    '   In:   varPhrase = "A !@#$%^&*()_+-={}[]\|"':;?/<>,.`~ There"
    '         strFlag = "A"
    '   Out:  A There
    '************************************************************************************
    Dim intPhraseLength As Integer
    Dim intCount As Integer
    Dim strFinal As String
    Dim strCleansed As String
    Dim blnSwitch As Boolean
    Dim strLetter As String

    If IsNull(varPhrase) Then Exit Function
    If IsNull(strFlag) Then strFlag = "A"
    If strFlag <> "C" And strFlag <> "M" And strFlag <> "O" And strFlag <> "P" And strFlag <> "A" Then
        Beep
        MsgBox "strFlag must be C,M,O,P or A", 16, "Oops"
        Exit Function
    End If

    intPhraseLength = Len(varPhrase)
    blnSwitch = False
    For intCount = 1 To intPhraseLength
        strLetter = Mid(varPhrase, intCount, 1)
        Select Case strFlag
            Case "C"
                GoSub C_NoPunc
            Case "M"
                GoSub M_NoPunc
            Case "O"
                GoSub O_NoPunc
            Case "P"
                GoSub P_NoPunc
            Case "A"
                GoSub C_NoPunc
                If blnSwitch = False Then
                    GoSub M_NoPunc
                    If blnSwitch = False Then
                        GoSub O_NoPunc
                        If blnSwitch = False Then
                            GoSub P_NoPunc
                        End If
                    End If
                End If
        End Select
        If blnSwitch = True Then
            strCleansed = ""
        Else
            strCleansed = strLetter
        End If
        strFinal = strFinal & strCleansed
    Next intCount
    NoPunc = strFinal
Exit_NoPunc:
    Exit Function
C_NoPunc:
    If strLetter = "(" Then
        blnSwitch = True
    ElseIf strLetter = ")" Then
        blnSwitch = True
    ElseIf strLetter = "[" Then
        blnSwitch = True
    ElseIf strLetter = "]" Then
        blnSwitch = True
    ElseIf strLetter = "{" Then
        blnSwitch = True
    ElseIf strLetter = "}" Then
        blnSwitch = True
    Else
        blnSwitch = False
    End If
    Return
M_NoPunc:
    If Asc(strLetter) = 34 Then         'Check for "
        blnSwitch = True
    ElseIf strLetter = "@" Then
        blnSwitch = True
    ElseIf strLetter = "#" Then
        blnSwitch = True
    ElseIf strLetter = "$" Then
        blnSwitch = True
    ElseIf strLetter = "%" Then
        blnSwitch = True
    ElseIf strLetter = "^" Then
        blnSwitch = True
    ElseIf strLetter = "&" Then
        blnSwitch = True
    ElseIf strLetter = "|" Then
        blnSwitch = True
    ElseIf strLetter = "'" Then
        blnSwitch = True
    ElseIf strLetter = "`" Then
        blnSwitch = True
    ElseIf strLetter = "~" Then
        blnSwitch = True
    ElseIf strLetter = "_" Then
        blnSwitch = True
    ElseIf strLetter = " " Then
        blnSwitch = True
    Else
        blnSwitch = False
    End If
    Return
O_NoPunc:
    If strLetter = "<" Then
        blnSwitch = True
    ElseIf strLetter = ">" Then
        blnSwitch = True
    ElseIf strLetter = "*" Then
        blnSwitch = True
    ElseIf strLetter = "+" Then
        blnSwitch = True
    ElseIf strLetter = "-" Then
        blnSwitch = True
    ElseIf strLetter = "=" Then
        blnSwitch = True
    ElseIf strLetter = "\" Then
        blnSwitch = True
    ElseIf strLetter = "/" Then
        blnSwitch = True
    Else
        blnSwitch = False
    End If
    Return
P_NoPunc:
    If strLetter = ":" Then
        blnSwitch = True
    ElseIf strLetter = ";" Then
        blnSwitch = True
    ElseIf strLetter = "?" Then
        blnSwitch = True
    ElseIf strLetter = "," Then
        blnSwitch = True
    ElseIf strLetter = "." Then
        blnSwitch = True
    ElseIf strLetter = "!" Then
        blnSwitch = True
    Else
        blnSwitch = False
    End If
    Return
Err_NoPunc:
    MsgBox Err & ": " & Error$, 16, "test"
    Resume Exit_NoPunc
End Function
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about this:


Public Function NoPunc(varPhrase As Variant, strFlag As String) As String
Dim intPhraseLength As Integer
Dim intCount As Integer
Dim strFinal As String
Dim strCleansed As String
Dim blnSwitch As Boolean
Dim strLetter As String
Dim C As String, M As String, O As String, P As String, A As String
C = "(){}[]"
M = """""@#$%^&|'`~_"
O = "-+=*/\"
A = C & M & O
strFlag = UCase(strFlag)
If IsNull(varPhrase) Then Exit Function
If IsNull(strFlag) Then strFlag = "A"
If strFlag <> "C" And strFlag <> "M" And strFlag <> "O" And strFlag <> "P" And strFlag <> "A" Then
Beep
MsgBox "strFlag must be C,M,O,P or A", 16, "Oops"
Exit Function
End If
intPhraseLength = Len(varPhrase)
blnSwitch = False
For intCount = 1 To intPhraseLength
strLetter = Mid(varPhrase, intCount, 1)
Select Case strFlag
Case "C"
If InStr(C, strLetter) > 0 Then blnSwitch = True
Case "M"
If InStr(M, strLetter) > 0 Then blnSwitch = True
Case "O"
If InStr(0, strLetter) > 0 Then blnSwitch = True
Case "P"
If InStr(P, strLetter) > 0 Then blnSwitch = True
Case "A"
If InStr(A, strLetter) > 0 Then blnSwitch = True
End Select
If blnSwitch = True Then
strCleansed = ""
Else
strCleansed = strLetter
End If
strFinal = strFinal & strCleansed
blnSwitch = False
Next intCount
NoPunc = strFinal
End Function
 
Upvote 0
Close, but I don't get the same result as before.

For example

using the test case

NoPunc("A !@#$%^&*()_+-={}[]\| ':;?/<>,.`~ There","A")

Old Code Yields:
AThere

New Code Yields:
A ! :;?<>,. There which is incorrect.

edit:

I see that the P statement is missing:

P = ",.?;:!"
A = C & M & O & P

This should now yield
A There

Which is correct!
CT
 
Upvote 0

Forum statistics

Threads
1,221,864
Messages
6,162,497
Members
451,770
Latest member
tsalaki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top