Multiple Apps

Remove (undesired) repeated characters from target string

Ease of Use

Intermediate

Version tested with

2000, 2003 

Submitted by:

MWE

Description:

Undesired/unnecessary repeated characters are removed from target string 

Discussion:

Removing repeated characters, more properly reducing the number of repeats of a character to some desired value, is a useful string management tool. The VB/VBA Replace function will replace, say, all instances of two blanks in a row with a single blank, but is not so useful when the number of ?extra? blanks is not known. RemoveExtra, a string function, will perform this task. It has three arguments: the target string; the target character {optional, default = ? ?}; and, the number of ?repeats? of that character allowed {optional, default = 1}. If the number of repeats is set to 0, the result is a string with no instances of the target character. RemoveExtra can be used for any VBA application. The demo is for Excel and Word. 

Code:

instructions for use

			

Option Explicit Sub xlRemoveExtra() ' '**************************************************************************************** ' Function: demonstrates the RemoveExtra function for Excel '**************************************************************************************** ' ' 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 ' '**************************************************************************************** ' Function removes extra repeated characters from a target string. The revised ' string is returned as the functional value. ' Passed Values: ' strText [in, string] target string to be examined ' Char [in, string, OPTIONAL] target character {default = " "} ' Num [in, long, OPTIONAL] number of allowable repetitions {default = 1} ' ' NOTES: let strOld = "now is the time for all good men to come to the aid of their party" ' ' note the two blank between "the" and "time" ' ' we set strNew = RemoveExtra(strOld, " ", 1) then ' strNew is now = "now is the time for all good men to come to the aid of their party" '**************************************************************************************** ' ' Dim OrigLen As Long ' ' ensure acceptable value for Num ' If Num < 0 Then MsgBox "RemoveExtra: value for Num is not valid", "vbCritical + vbOKOnly" Exit Function End If ' ' copy original text string into RemoveExtra ' keep calling Replace with repeated (target) char string of length Num+1 ' replaced by similar string of length Num until result does not change ' RemoveExtra = strText Do Until Len(RemoveExtra) = OrigLen OrigLen = Len(RemoveExtra) RemoveExtra = Replace(RemoveExtra, String(Num + 1, Char), String(Num, Char)) Loop End Function ' ' ************************************************************ ' WORD code ' ************************************************************ Sub wrdRemoveExtra() ' '**************************************************************************************** ' Function: demonstrates the RemoveExtra function for Word '**************************************************************************************** ' ' 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 ' '**************************************************************************************** ' Function removes extra repeated characters from a target string. The revised ' string is returned as the functional value. ' Passed Values: ' strText [in, string] target string to be examined ' Char [in, string, OPTIONAL] target character {default = " "} ' Num [in, long, OPTIONAL] number of allowable repetitions {default = 1} ' ' NOTES: let strOld = "now is the time for all good men to come to the aid of their party" ' ' note the two blank between "the" and "time" ' ' we set strNew = RemoveExtra(strOld, " ", 1) then ' strNew is now = "now is the time for all good men to come to the aid of their party" '**************************************************************************************** ' ' Dim OrigLen As Long ' ' ensure acceptable value for Num ' If Num < 0 Then MsgBox "RemoveExtra: value for Num is not valid", "vbCritical + vbOKOnly" Exit Function End If ' ' copy original text string into RemoveExtra ' keep calling Replace with repeated (target) char string of length Num+1 ' replaced by similar string of length Num until result does not change ' RemoveExtra = strText Do Until Len(RemoveExtra) = OrigLen OrigLen = Len(RemoveExtra) RemoveExtra = Replace(RemoveExtra, String(Num + 1, Char), String(Num, Char)) Loop End Function

How to use:

  1. Copy the above code.
  2. Open any workbook.
  3. Press Alt + F11 to open the Visual Basic Editor (VBE).
  4. In the left side window, hi-lite the target spreadsheet [it will likely be called VBAProject(name.xls) where name is the name of the spreadsheet]
  5. Select an existing code module for the target worksheet; or from the Insert Menu, choose Insert | Module.
  6. Paste the code into the right-hand code window.
  7. Add user-defined code as appropriate
  8. Close the VBE, save the file if desired.
  9. See ?Test The Code? below
 

Test the code:

  1. EXCEL: open the xls file
  2. Enter text (containing some repeated character) in cell B2
  3. Click on the yellow command button and answer the two questions about target character and # of repeats allowed
  4. The results are posted to cell B4
  5. WORD: open the doc file
  6. Enter some text (containing some repeated character) and select it
  7. Click on the yellow command box and answer the two questions about target character and # of repeats allowed
  8. The results are written back to the selection
 

Sample File:

RemoveExtra.zip 38.8KB 

Approved by mdmackillop


This entry has been viewed 89 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express