Option Explicit
Sub xlRemoveExtra()
Dim Char As String
Dim MsgBxRtn As VbMsgBoxResult
Dim MsgBxTitle As String
Dim Num As Long
Dim strNum As String
Dim strText As String
MsgBxTitle = "xlRemoveExtra"
GetChar:
Char = InputBox("test or target character?", MsgBxTitle)
If Char = "" Then Exit Sub
GetNum:
strNum = InputBox("# of test chars allowed?", MsgBxTitle)
Select Case strNum
Case vbNullString, ""
GoTo GetChar
Case Is < 0
MsgBox "# must be >= 0", vbCritical + vbOKOnly
GoTo GetNum
Case Is = 0
MsgBxRtn = MsgBox("a zero value will effectively remove all" & vbCrLf & _
"instances of the target character. OK?", _
vbQuestion + vbYesNoCancel, MsgBxTitle)
If MsgBxRtn <> vbYes Then GoTo GetNum
Num = strNum
Case Else
Num = strNum
End Select
strText = Cells(2, 2).Text
Cells(4, 2) = RemoveExtra(strText, Char, Num)
End Sub
Function RemoveExtra(strText As String, _
Optional Char As String = " ", _
Optional Num As Long = 1) As String
Dim OrigLen As Long
If Num < 0 Then
MsgBox "RemoveExtra: value for Num is not valid", "vbCritical + vbOKOnly"
Exit Function
End If
RemoveExtra = strText
Do Until Len(RemoveExtra) = OrigLen
OrigLen = Len(RemoveExtra)
RemoveExtra = Replace(RemoveExtra, String(Num + 1, Char), String(Num, Char))
Loop
End Function
Sub wrdRemoveExtra()
Dim Char As String
Dim MsgBxRtn As VbMsgBoxResult
Dim MsgBxTitle As String
Dim Num As Long
Dim strNum As String
Dim strText As String
MsgBxTitle = "xlRemoveExtra"
GetChar:
Char = InputBox("test or target character?", MsgBxTitle)
If Char = "" Then Exit Sub
GetNum:
strNum = InputBox("# of test chars allowed?", MsgBxTitle)
Select Case strNum
Case vbNullString, ""
GoTo GetChar
Case Is < 0
MsgBox "# must be >= 0", vbCritical + vbOKOnly
GoTo GetNum
Case Is = 0
MsgBxRtn = MsgBox("a zero value will effectively remove all" & vbCrLf & _
"instances of the target character. OK?", _
vbQuestion + vbYesNoCancel, MsgBxTitle)
If MsgBxRtn <> vbYes Then GoTo GetNum
Num = strNum
Case Else
Num = strNum
End Select
strText = Selection.Text
Selection.Text = RemoveExtra(strText, Char, Num)
End Sub
Function RemoveExtra(strText As String, _
Optional Char As String = " ", _
Optional Num As Long = 1) As String
Dim OrigLen As Long
If Num < 0 Then
MsgBox "RemoveExtra: value for Num is not valid", "vbCritical + vbOKOnly"
Exit Function
End If
RemoveExtra = strText
Do Until Len(RemoveExtra) = OrigLen
OrigLen = Len(RemoveExtra)
RemoveExtra = Replace(RemoveExtra, String(Num + 1, Char), String(Num, Char))
Loop
End Function
|