我試圖運行一個代碼,將在一個文件夾中的所有excel文件中進行一些替換。首先,這裏是代碼:爲什麼我得到一個不存在的文件名?
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
但在某一點上,一些替代品後,它給了我一個錯誤說,它不能打開文件:
「的Excel無法打開文件'〜$ PLAN COMPARAT de CUSTOS Rancho Bom Loja2-2015.xlsx',因爲文件格式或文件擴展名無效,請檢查文件是否損壞,以及擴展名是否與文件格式匹配。 (這是一個來自葡萄牙語的錯誤消息的翻譯,對不起,我無法改變語言)
事實是我在這個文件夾上搜索過,並且沒有這樣的文件。有一個名爲「PLAN COMPARAT de CUSTOS RB-L2-2015.xlsx」的文件,它是一個縮寫。也許它試圖打開的文件是不存在的舊文件?
我有一個腳本,以前在word文件中做替換,它也會嘗試打開一些不存在的文件,或者嘗試打開一個名爲「〜$」的文件加上一個名稱存在的文件。這個「〜$」從哪裏來?
我需要一些線索。
什麼是 「.xlsl」?我期望「.xlsx」/「。xlsxm」/「。xls」。 –
'.xlsl'是文件的鎖定版本。當Excel在某些時候崩潰並且它隱藏在驅動器中時,最有可能保存該文件。如果您選擇在Internet Explorer中顯示隱藏文件的選項,您將看到它。你可以做一個我相信測試隱藏文件的檢查(或者至少在文件名中檢查'〜$'或'.xlsl',如果有的話跳過它(或者甚至刪除它) –
@ Grade'Eh'Bacon - 看到我的評論關於'.xlsl' –