This is a weird one. I am trying to replace the second occurrence of a value using the Replace
function. This is what I have.
Sub_string = Replace(sub_string, "<ul>", "", ulnestfound, 1, vbTextCompare)
My thinking is that the Replace
function would start looking at position ulnestfound
, and replace the first occurrence of "<ul>"
from that point. What’s actually happening is that all of the text prior to ulnestfound
is being deleted, along with the <ul>
tag. So, for example, if ulnestfound = 100
, the first 100 characters of sub_string
are being deleted,and then the replacement is made.
Any ideas why this is happening? It’s driving me mad.
I’ve tried manually keying the start value, which is how I realized what was happening. I can’t imagine why it would delete text like that.
The documentation of the Replace function says:
Returns a string, which is a substring of a string expression beginning at the start position (defaults to 1), in which a specified substring has been replaced with another substring a specified number of times.
I agree that it doesn't do what most people would assume, but it is consistent with the observed behavior.
You can instead write:
Sub_string = Left(sub_string, ulnestfound - 1) & _
Replace(sub_string, "<ul>", "", ulnestfound, 1,, vbTextCompare)
Note that the syntax is
Replace(expression, find, replace, [ start, [ count, [ compare ]]])
You omited the count
parameter. Either write
Sub_string = Replace(sub_string, "<ul>", "", ulnestfound, 1,, vbTextCompare)
or
Sub_string = Replace(sub_string, "<ul>", "", ulnestfound, 1, Compare:=vbTextCompare)
otherwise vbTextCompare
(=1) will be taken as count
.