Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting characters and/or blank (empty) spaces 1

Status
Not open for further replies.

tyhand

Programmer
Jul 3, 2002
186
US
Hi all,

How would I be able to delete a character or blank (empty)
spaces within a variable? I've used Trim, LTrim and Mid,
but it doesn't work with the specific value I need to change.

I've created a Macro to search for a specific
number format using MS Word 97. If the specific number format is found, then the actual number is selected and passed to a variable. The number found is in format #anynumber, anynumber...

I need to delete the # (pound) sign after finding the number.

I've provided a sample code and comments for analysis.
Thanks in advance for the help. Peace!

SAMPLE CODE:

' The number format to search for. Note: ^# = any number
' example: #^#^#^#^#^# might equal #12345
Invoice7 = "#^#^#^#^#^#^#^#"

' The search
Set SearchInv = ActiveDocument.Content
SearchInv.Find.Execute FindText:=Invoice7 Forward:=True
If SearchInv.Find.Found = True Then
SearchInv.Select

' The variable name if number format is found
Dim InvDocName
InvDocName = SearchInv

' The Function to search for the # sign and delete it.
' ***This is where I need help.***

Private Function CleanInv(TheInv)
' use LEFT to check first character of number
' for # (pound) sign
If Left(TheInv, 1) = "#" Then
' if found, use mid to replace the # sign with an empty space
' *This is where I have a problem as using Mid Statement gets
' rid of the # (pound) sign, but then creates an empty space*

Mid(TheInv, 1, 1) = ""

' finally, delete the empty space using TRIM function
' and return the value of the function
' Note: This however, does not delete the empty space
TrimmedInv = Trim(TheInv)
CleanInv = TrimmedInv
Else
CleanInv = TheInv
End If
End Function

' NOTE: The FUNCTION 'CleanInv' works perfectly
' with the SUB below. However, it doesn't work at
' all in the above example.

Sub Testing123()

InvDocName = "#7772225"
Call CleanInv(TheInv)
Debug.Print CleanInv(InvDocName)

End Sub

' InvDocName will then equal "7772225"

Thanks again for any help. Peace!
 
Try this, it will give you a new string starting with byte two. I think you are only checking the first byte for #. If I misunderstood your question, my email address is in my profile.

If Left(theInv, 1) = "#" then
theInv = Mid(theInv, 2)
End If

Have a great day!
 
Kudos to SBendBuckeye. It worked!

I don't know why I didn't see it from that angle before.

I'm still curious though about deleting a character or
empty space within a value if it's possible.

I was actually thinking of next using
selection.moveleft() or selection.delete() or
whatever(whatever).

If you've used the above effectively within this scope
please let me know. Thanks again.

Peace!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top