Excel 2016 VBA Replace Error Failing Inconsistently

120 Views Asked by At

I am trying to loop through worksheets and remove file location prefixes using the Worksheet.Cells.Replace method in an Auto_Open function in Excel 2016 32-bit.

ws.Cells.Replace What:="'*:\*addin_name.xla*'!", Replacement:="", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
        SearchFormat:=False, ReplaceFormat:=False

The line above will occasionally cause a Run-time error '13': Type mismatch. Other times, the macro will run smoothly.

Has anyone experienced this behavior? Is there a good solution? If not, how could I structure the code to try the Replace method again 4 times if it causes the error?

0

There are 0 best solutions below