In code, the ".Find" method sometimes works and sometimes seems to fail for no reason I can see.
I have an add-in, for use in Excel, which several of my colleagues use and which I also use in several different locations. There is a problem in Excel (previously discussed in other threads) whereby it saves explicit path references to add-ins, so that if a user who references an add-in gives a copy of the workbook to another user who stores his copy of the same add-in in a different location then the workbook will not call his version of the add-in.
I have fixed this problem by writing a macro which is part of my add-in.
When run, the macro looks for all instances of a particular string which is a "signature" of the use of an explicit path for the add-in in question. It then parses the text to find the path to the add-in. It then does a search and replace, stripping the explicit path reference from all the cells which call the add-in functions. At this point, the new user's workbook will then identify the function call with its own version of the add-in.
That is all very well, and usually works fine. Except occasionally it does not seem to do so. Sometimes, it does not report finding any matches, even when they are clearly there.
I just ran and debugged the code in one of these cases and the "c" variable - the recipient of the find process - is listed as "nothing" or "error 2029" depending apparently on whether an instance of the search-for text is found.
Can anybody throw any light on why the behaviour of this code should vary, why the Find method sometimes seems to give these anomalous results, and if there is a solution to the problem?
Thanks in advance,
Tony
Here is the code:
I have an add-in, for use in Excel, which several of my colleagues use and which I also use in several different locations. There is a problem in Excel (previously discussed in other threads) whereby it saves explicit path references to add-ins, so that if a user who references an add-in gives a copy of the workbook to another user who stores his copy of the same add-in in a different location then the workbook will not call his version of the add-in.
I have fixed this problem by writing a macro which is part of my add-in.
When run, the macro looks for all instances of a particular string which is a "signature" of the use of an explicit path for the add-in in question. It then parses the text to find the path to the add-in. It then does a search and replace, stripping the explicit path reference from all the cells which call the add-in functions. At this point, the new user's workbook will then identify the function call with its own version of the add-in.
That is all very well, and usually works fine. Except occasionally it does not seem to do so. Sometimes, it does not report finding any matches, even when they are clearly there.
I just ran and debugged the code in one of these cases and the "c" variable - the recipient of the find process - is listed as "nothing" or "error 2029" depending apparently on whether an instance of the search-for text is found.
Can anybody throw any light on why the behaviour of this code should vary, why the Find method sometimes seems to give these anomalous results, and if there is a solution to the problem?
Thanks in advance,
Tony
Here is the code:
Code:
Sub AL_Clear_AL_XL_Refs()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Description:
'This sub finds and removes all the explicit path references to AL_XL_Tools.xla in the active workbook.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'dim vars
Dim Pathfound As Boolean, searchingsheet
Dim AL_Path As String, stringstart As String
Dim wbk As Workbook
Dim wksht As Worksheet
Dim numshts As Long, shtnum As Long, funcpos As Long, startpos As Long
Dim c As Variant
Dim oldstate As Variant
'note old dislay state so as to be able to restore it later, then set it to false to avoid messages during operation
oldstate = Application.DisplayAlerts
Application.DisplayAlerts = False
'check all worksheets for an instance of an explicit reference to AL_XL_Tools (if there is one) and note the path
Set wbk = ActiveWorkbook
Pathfound = False
numshts = wbk.Worksheets.count
shtnum = 0
On Error Resume Next
While Not Pathfound And shtnum < numshts
shtnum = shtnum + 1
Set wksht = wbk.Worksheets(shtnum)
searchingsheet = True
While searchingsheet
Set c = wksht.Cells.Find(What:="'!AL_", LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Err.Number = 0 Then
funcpos = InstrRev(c.Formula, "'!AL_")
stringstart = Mid(c.Formula, 1, funcpos - 1)
startpos = InstrRev(stringstart, "'")
If startpos > 0 Then
AL_Path = Mid(c.Formula, startpos, funcpos + 2 - startpos)
Pathfound = True
searchingsheet = False
Else
searchingsheet = True
End If
Else
searchingsheet = False
Err.Clear
End If
Wend
Wend
Err.Clear
On Error Resume Next
'find and remove all instances of the AL_XL_Tools path
If Pathfound Then
shtnum = 0
While shtnum < numshts
shtnum = shtnum + 1
wbk.Worksheets(shtnum).Cells.Replace What:=AL_Path, Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Err.Clear
Wend
MsgBox "All instances cleaned OK", vbOKOnly
Else