ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Replace object causes an error if search text not found (https://www.excelbanter.com/excel-programming/365422-replace-object-causes-error-if-search-text-not-found.html)

scott

Replace object causes an error if search text not found
 
Hello!

I have an issue that doesn't seem like it would be a big deal, but it is
causing major headaches. I should say that I am using Excel 2003:

What I need to do is have a standard Excel document that a user can modify
to include text that will be replaced with information from a database. This
works great currently. How I do it, is the user will place the field code as
a string in the body of a template Excel template (for example: person_name).
When the user clicks a button in my ASP page, I call the File System Object
and copy the template document to a location on the server. This works great!

Now, I want to replace the string in the copied document with that of a
field value as generated from the ASP page. This works great also!

I want to allow the user to add or remove strings from the Excel template as
they see fit, but currently, if they remove a field from the template, it is
obviously not available in the copied document as outlined above. This causes
an error!!!! i.e. if the replacement text is not in the Excel workbook, my
code will progress no further! I have tried to use .EnableEvents = False but
this doesn't work either.

What I need it to do is if it doesn't find the search string, to just move
on. Here is some partial code (its in JScript but I converted it here to
VBScript):

Set objRange = xlApp.ActiveWorkbook.Worksheets(w).UsedRange
objRange.Select
objRange.Replace( strFindText, strReplaceText)

--


Thanks!

Scott

Jim Thomlinson

Replace object causes an error if search text not found
 
XL2002 and above returns an alert if nothing is found to replace. Try
disabling the alerts...

application.displayalerts = false
objRange.Replace( strFindText, strReplaceText)
application.displayalerts = true
--
HTH...

Jim Thomlinson


"Scott" wrote:

Hello!

I have an issue that doesn't seem like it would be a big deal, but it is
causing major headaches. I should say that I am using Excel 2003:

What I need to do is have a standard Excel document that a user can modify
to include text that will be replaced with information from a database. This
works great currently. How I do it, is the user will place the field code as
a string in the body of a template Excel template (for example: person_name).
When the user clicks a button in my ASP page, I call the File System Object
and copy the template document to a location on the server. This works great!

Now, I want to replace the string in the copied document with that of a
field value as generated from the ASP page. This works great also!

I want to allow the user to add or remove strings from the Excel template as
they see fit, but currently, if they remove a field from the template, it is
obviously not available in the copied document as outlined above. This causes
an error!!!! i.e. if the replacement text is not in the Excel workbook, my
code will progress no further! I have tried to use .EnableEvents = False but
this doesn't work either.

What I need it to do is if it doesn't find the search string, to just move
on. Here is some partial code (its in JScript but I converted it here to
VBScript):

Set objRange = xlApp.ActiveWorkbook.Worksheets(w).UsedRange
objRange.Select
objRange.Replace( strFindText, strReplaceText)

--


Thanks!

Scott



All times are GMT +1. The time now is 05:36 PM.

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