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
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