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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I simulate the VB Replace Function in VBA?

Functions

How do I simulate the VB Replace Function in VBA?

by  bustell  Posted    (Edited  )
Here is a function that works with Access to simulate the built in VB Replace function. For those of you have have trouble with recursion, this function does not use it which makes it easier to understand.

It also allows you to set the string comparison method. The compare argument can be omitted, or it can be 0 or 1. Specify 0 (default) to perform a binary comparison. Specify 1 to perform a textual, noncase-sensitive comparison.

Public Function Replace(sString As Variant _
, sFind As String _
, sReplace As String _
, Optional iCompare As Long = vbBinaryCompare)
_ As Variant

Dim iStart As Integer, iLength As Integer
If IsNull(sString) Then
Replace = Null
Else
iStart = InStr(1, sString, sFind, iCompare)
Do While iStart > 0
sString = Left(sString, iStart - 1) _
& sReplace & Mid(sString, iStart + Len(sFind), Len(sString) - iStart - Len(sFind) + 1)
iStart = InStr(iStart, sString, sFind)
Loop
Replace = sString
End If

End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top