Hi, Hopefully a simple one.. I'm struggling to handle #VALUE! in VBA.
I've a simple bit of code, and am using the worksheet FIND formula.. However it won't handle the result coming back if FIND doesn't find a string..
here is the example:
Function CleanTxt(myText As String) As String
If IsError(WorksheetFunction.Find("OLD", myText)) Then
CleanTxt = myText
Else
x = WorksheetFunction.Find("OLD", myText)
CleanTxt = WorksheetFunction.Replace(myText, x, 3, "NEW")
End If
End Function
In my spreadsheet I call the formula...=CleanTxt(C3) (C3 contains the old text and the formula is in it..)
and the result is:
thisisOLDtext thisisNEWtext
thisisnt #VALUE!
Although I just want it to gracefully exit, no matter what I try I cannot get around the #VALUE turning up in the field where it doesn't find the matching text.. (and telling me that the data is of the wrong datatype.
I know there are alternatives to this particular find / replace, but I'm trying to get to grips with using Excel Formulas, and actually trying to work out why I cannot handle the error correctly.
Many Thanks
Rich
I've a simple bit of code, and am using the worksheet FIND formula.. However it won't handle the result coming back if FIND doesn't find a string..
here is the example:
Function CleanTxt(myText As String) As String
If IsError(WorksheetFunction.Find("OLD", myText)) Then
CleanTxt = myText
Else
x = WorksheetFunction.Find("OLD", myText)
CleanTxt = WorksheetFunction.Replace(myText, x, 3, "NEW")
End If
End Function
In my spreadsheet I call the formula...=CleanTxt(C3) (C3 contains the old text and the formula is in it..)
and the result is:
thisisOLDtext thisisNEWtext
thisisnt #VALUE!
Although I just want it to gracefully exit, no matter what I try I cannot get around the #VALUE turning up in the field where it doesn't find the matching text.. (and telling me that the data is of the wrong datatype.
I know there are alternatives to this particular find / replace, but I'm trying to get to grips with using Excel Formulas, and actually trying to work out why I cannot handle the error correctly.
Many Thanks
Rich