I'm trying to run a code that will do some replaces in all excel files in a folder. First, here is the code:
Dim FileSystem As Object
Dim HostFolder As String
Dim StringA As String
Dim StringB As String
Sub Init()
Application.DisplayAlerts = False
HostFolder = "\\Pc-dell\d\Documentos\LSM2\CLIENTES\SRB CDT 2 PROGRESSO2\"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
Application.DisplayAlerts = True
End Sub
Sub DoFolder(Folder)
Dim SubFolder
For Each SubFolder In Folder.SubFolders
DoFolder SubFolder
Next
Dim File
For Each File In Folder.Files
If Right(File.Name, 4) = ".xls" Or Right(File.Name, 5) = ".xlsx" Then
With Workbooks.Open(File, False)
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
SwapStringsInActiveWorkbook "l1", "l2", ws
SwapStringsInActiveWorkbook "L1", "L2", ws
SwapStringsInActiveWorkbook "l 1", "l 2", ws
SwapStringsInActiveWorkbook "L 1", "L 2", ws
SwapStringsInActiveWorkbook "loja1", "loja2", ws
SwapStringsInActiveWorkbook "LOJA1", "LOJA2", ws
SwapStringsInActiveWorkbook "loja 1", "loja 2", ws
SwapStringsInActiveWorkbook "LOJA 1", "LOJA 2", ws
SwapStringsInActiveWorkbook "Loja1", "Loja2", ws
SwapStringsInActiveWorkbook "Loja 1", "Loja 2", ws
SwapStringsInActiveWorkbook "LOJA1", "LOJA2", ws
SwapStringsInActiveWorkbook "LOJA 1", "LOJA 2", ws
Next ws
End With
ActiveWorkbook.Save
ActiveWorkbook.Close
End If
Continue:
Next
End Sub
Sub SwapStringsInActiveWorkbook(StringA As String, StringB As String, ws As Worksheet)
On Error Resume Next
ws.Cells.SpecialCells(xlCellTypeConstants).Replace What:=StringA, Replacement:="_AUXTEMPREPL_", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
On Error Resume Next
ws.Cells.SpecialCells(xlCellTypeConstants).Replace What:=StringB, Replacement:=StringA, LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
On Error Resume Next
ws.Cells.SpecialCells(xlCellTypeConstants).Replace What:="_AUXTEMPREPL_", Replacement:=StringB, LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
On Error GoTo 0
End Sub
But at a certain point, after some replacements, it gives me an error saying that it couldn't open a file:
"Excel couldn't open the file '~$PLAN COMPARAT de CUSTOS Rancho Bom Loja2-2015.xlsx' because the file format or the file extension is not valid. Check if the file is not corrupted and if the extension matches the file format." (this is a translation from a error message that was in portuguese, sorry I couldn't change the language)
The fact is that I did a search on this folder, and there is no such file. There is a file called "PLAN COMPARAT de CUSTOS RB-L2-2015.xlsx" though, which is an abbreviation. Maybe the file it is trying to open is and old file that doesn't exist anymore?
I had a script to do replacements in word files before as well, and it would also try to open some files that doesn't exist or it would try to open a file called "~$" plus the name of a file that existed. Where is this "~$" coming from?
I need some clues about that.