2022年5月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
無料ブログはココログ

« VBAのReplace()関数も、チョー遅い! | トップページ | VBScriptやVBAのSplit()関数も、チョー遅い! »

2008年5月 5日 (月)

VBAのReplace()関数の代替関数

これも同様です。性能は、中くらい!
なので、これより、"VBScript.RegExp"のReplace()メソッドがお勧めです。

a = String(1024& * 1024&, "a")
t1 = Timer
b = Replacex(a, "a", "b")
Debug.Print Timer - t1
' 2.523438

Function Replacex(s, p, r)
Dim a() As String, n As Long, b As Long, e As Long, f As Long
ReDim a(Len(s))
b = 1
n = 0
f = InStr(b, s, p)
Do While f
  a(n) = Mid(s, b, f - b)
  n = n + 1
  b = f + Len(p)
  f = InStr(b, s, p)
Loop
a(n) = Mid(s, b)
ReDim Preserve a(n)
Replacex = Join(a, r)
End Function

« VBAのReplace()関数も、チョー遅い! | トップページ | VBScriptやVBAのSplit()関数も、チョー遅い! »