ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to tell if Replace() did a replacement? (https://www.excelbanter.com/excel-programming/357900-how-tell-if-replace-did-replacement.html)

[email protected]

How to tell if Replace() did a replacement?
 
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



Vic Eldridge[_3_]

How to tell if Replace() did a replacement?
 
I'd like to figure out why the Replace() keeps returning True.

Because Replace returns a string, not a boolean.


Regards,
Vic Eldridge


" wrote:

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




Tim W

How to tell if Replace() did a replacement?
 
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


"Vic Eldridge" wrote in message
...
I'd like to figure out why the Replace() keeps returning True.


Because Replace returns a string, not a boolean.


Regards,
Vic Eldridge


" wrote:

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






Tim W

How to tell if Replace() did a replacement?
 
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.


wrote in message
oups.com...
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






All times are GMT +1. The time now is 07:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com