Excel

Replace Function for VB5 (Office 97)

Ease of Use

Intermediate

Version tested with

97,2003 

Submitted by:

Oorang

Description:

Allows you to make your VB6 code backward compatible by providing a replace function for VB5. Uses byte arrays to improve performance. 

Discussion:

Making VBA written in Office 2000 (VB6) or greater backwards compatible with VB5 (Office 97) and prior. 

Code:

instructions for use

			

Option Explicit Option Base 0 'Code Written by Aaron Bush - 05/04/2007 'Free for Public Use #If Not VB6 Then Public Const vbBinaryCompare As Long = 0 Public Const vbTextCompare As Long = 1 Public Function Replace(Expression As Variant, Find As Variant, ReplaceWith As Variant, Optional Start As Long = 1, Optional Count As Long = -1, Optional Compare As Long) Dim byExpression() As Byte 'Byte Array holding "Expression" argument. Dim byFind() As Byte 'Byte Array holding "Find" argument. Dim byRplcW() As Byte 'Byte Array holding "ReplaceWith" argument. Dim byTmp() As Byte 'Byte Array holding return value until completion. 'Array Indexes: Dim lExpPos As Long Dim lFindPos As Long Dim lRplcPos As Long Dim lTmpPos As Long 'Array Lower Bounds: Dim lExprLB As Long 'All others use 0 'Array Upper-Bounds: Dim lExprUB As Long Dim lFindUB As Long Dim lRplcUB As Long Dim lTmpUB As Long 'String Versions of Arguments: Dim sExpr As String Dim sFind As String 'Counter(s): Dim lRplcCnt As Long 'Counts the number of replacments made. Const lNoReplace_c As Long = -1 Const lZero_c As Long = 0 Const lArrayOffset_c As Long = 1 Const lUnicodeOffset_c As Long = 2 If Count = lZero_c Then Exit Function If Compare = vbBinaryCompare Then 'The comparisons this function makes are of ASCII values, 'so no action is needed to make a binary comparison. byExpression = CStr(Expression) byFind = CStr(Find) Else 'As a Q&D way to do text comparison all values are converted 'to lower case for comparison: sExpr = VBA.LCase$(CStr(Expression)) sFind = VBA.LCase$(CStr(Find)) byExpression = sExpr byFind = sFind End If If UBound(byFind) < lZero_c Then Replace = CStr(byExpression) Exit Function End If 'Set UpperBounds to variables as all are used in several places. lExprUB = UBound(byExpression) If Start > lExprUB + lArrayOffset_c Then Exit Function lFindUB = UBound(byFind) byRplcW = CStr(ReplaceWith) lRplcUB = UBound(byRplcW) lExprLB = Start - lArrayOffset_c Do For lExpPos = lExprLB To lExprUB Step lUnicodeOffset_c If byExpression(lExpPos) = byFind(lFindPos) Then lFindPos = lFindPos + lUnicodeOffset_c If lFindPos > lFindUB Then lFindPos = lZero_c Exit For End If Else lFindPos = lZero_c End If Next lExpPos 'Check to see if entire string has been scanned: If lExpPos > lExprUB Then Exit Do Else 'Do replacement: If Compare = vbTextCompare Then 'Restore Case: byExpression = sExpr byFind = sFind End If 'Set lowerbound for next replacement: lExprLB = lExpPos - lFindUB + lRplcUB 'Create Temp array for to hold replaced value while it 'is being built. lTmpUB = lExprUB - lFindUB + lRplcUB ReDim byTmp(lTmpUB) As Byte 'Load text before Found-text: For lTmpPos = lZero_c To lExpPos - lFindUB Step lUnicodeOffset_c byTmp(lTmpPos) = byExpression(lTmpPos) Next lTmpPos 'Load replacement text: lRplcPos = lZero_c For lTmpPos = lTmpPos To lTmpPos + lRplcUB Step lUnicodeOffset_c byTmp(lTmpPos) = byRplcW(lRplcPos) lRplcPos = lRplcPos + lUnicodeOffset_c Next lTmpPos 'Load text after Found-text: lExpPos = lExpPos + lUnicodeOffset_c For lTmpPos = lTmpPos To lTmpUB Step lUnicodeOffset_c byTmp(lTmpPos) = byExpression(lExpPos) lExpPos = lExpPos + lUnicodeOffset_c Next lTmpPos 'Save replaced value: byExpression = byTmp 'Prepare for next loop sExpr = CStr(byExpression) lExprUB = UBound(byExpression) If Count <> lNoReplace_c Then lRplcCnt = lRplcCnt + lArrayOffset_c If lRplcCnt = Start Then Exit Do End If End If Loop Replace = CStr(byExpression) End Function #End If

How to use:

  1. From the Visual Basic Editor Insert Module
  2. Paste code in module.
  3. If your code was originally for VB6 then and you have explicitly declared any Replace function calls using the VBA prefix (ex: VBA.Replace) use the VBE's find/replace feature to replace "VBA.Replace" with "Replace" for the entire project.
  4. Code using replace should now use the VB6 Repalce function where it is available and your compatability function when VBA.Replace is not available.
 

Test the code:

  1. Private Sub
  2. MsgBox Replace("FooBarBaz","o","O")
  3. End Sub
 

Sample File:

mdlCompatability.zip 1.44KB 

Approved by mdmackillop


This entry has been viewed 110 times.

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