How to tell if Replace() did a replacement?

  • Thread starter Thread starter tjw
  • Start date Start date
T

tjw

Hi all-
I've got some excel code that does a search & replace through a number
of files. it has a list of values & new values to replace those with,
then it opens one of the files, and iterates though the list of
replacements. once it's done cycling through the list of replacements,
it closes the open file and opens a new one, starting the cycle again.
this all works fine.

What doesn't work fine is: I can't seem to get it to tell me correctly
whether it found anything with the replace. some of the files won't
have any replacements within. I'd like to keep a list of which files
got replacements (just a count would be fine) and which didn't.

I've tried many variations on the following
y= range.replace("old","new")

no matter what, y returns True, whether it made a replacement or not.

I suppose I could do an Instr() first and then do the replacement if
instr() is not 0 then increment the counter but I'd like to figure out
why the Replace() keeps returning True.

TIA,
Tim
(e-mail address removed)
 
I'd like to figure out why the Replace() keeps returning True.

Because Replace returns a string, not a boolean.


Regards,
Vic Eldridge
 
And it returns True no matter what, whether it makes a replacement or not.
I'd like to be able to tell if it made any replacements so I can increment a
counter.

Thanks
 
I figured it out, I did a Find() first, and if that doesn't come back as
Nothing, then I increment the counter and do the Replace(). If the Find()
comes back as Nothing, I don't increment and don't replace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top