Removing blank characters in a text string (1 Viewer)

FrankC

New member
Local time
Today, 07:42
Joined
Feb 11, 2007
Messages
6
I have created an algorithm that creates an activation code based on a customer name intermixed with some standard characters. This works fine except that it sometimes creates a blank character in the body of the text string (eg the string may be "f7Hk d3ghx"). I would like to remove the blank character (in the example, between the "k" and the "d"). Has anyone any ideas how I might be able to do this.
 

selevanm

Matt
Local time
Today, 01:42
Joined
Mar 7, 2007
Messages
17
Here is what I do maybe it will help....

this one works if there is only one blank space in the string....
strTest = "abc def"
strFull = Replace(strTest, " ", "", 1, 1, vbTextCompare)
MsgBox strTest
MsgBox strFull


if you increase the count parameter to the length of the string you want to replace it will replace all instances of the blank space...
strTest = "abc d e f"
strFull = Replace(strTest, " ", "", 1, 10, vbTextCompare)
MsgBox strTest
MsgBox strFull

Hope this is what you were looking for....
 

raskew

AWF VIP
Local time
Today, 01:42
Joined
Jun 2, 2001
Messages
2,734
Hi -

If using A97 (which lacks the Replace() function) or you want to remove multiple characters from a string, you might give this a try.

Code:
Function ChopIt(pstr As String, ParamArray varmyvals() As Variant) As String
'*******************************************
'Purpose:   Remove a list of unwanted
'           characters from a string
'Coded by:  raskew
'Inputs:    From debug window:
'           1) ? chopit("123-45-6789", "-")
'           2) ? chopit(" the quick brown fox ", " ", "o")
'Output:    1) 123456789
'           2) thequickbrwnfx
'*******************************************

Dim strHold As String
Dim i       As Integer
Dim n       As Integer

    strHold = Trim(pstr)
    'check for entry
    If UBound(varmyvals) < 0 Then Exit Function
    For n = 0 To UBound(varmyvals())
       Do While InStr(strHold, varmyvals(n)) > 0
          i = InStr(strHold, varmyvals(n))
          strHold = Left(strHold, i - 1) & Mid(strHold, i + Len(varmyvals(n)))
       Loop
    Next n
    ChopIt = Trim(strHold)

End Function
 

Users who are viewing this thread

Top Bottom