The following function I wrote returns "" whenever I attempt to use it on ranges that contain formulas. Oddly, when I test what values are returned for rngPhrase
and rngTextBlocks
in the immediate window, it returns a correct result.
I can get a correct result for the first range by appending .Text
(instead of using the default .value
or even .value2
) for the first range, but this doesn't work for the Array.
What is happening here and how can I solve it?
Public Function SelectionReplacer(rngPhrase As Range, rngTextBlocks As Range) As String
Dim arr
Dim strTextBlocks As String
Dim strPhrase As String
Dim i As Integer, j As Integer
'set initial phrase (must be a single cell)
strPhrase = rngPhrase.Text
'Set text block array
arr = rngTextBlocks
For i = LBound(arr, 1) To UBound(arr, 1)
If InStr(1, strPhrase, arr(i, 1)) > 0 Then
SelectionReplacer = Replace(strPhrase, arr(i, 1), arr(i, 2))
End If
Next i
End Function
UPDATE Examples of Input
rngPhrase = Range("D34")
with the value in this cell being calculated by a vlookup function that returns a string "[Anrede] [FullName] verfügt über ein äußerst [Art1] und [Art2] Fachwissen, das [Gender1] stets effektiv und erfolgreich [Können1] konnte. [Können2]"
rngTextBlocks
has similar functions all of which have similar text elements returned by their respective vlookup functions.