VBA解決方案
- 此代碼運行在由
strStartFolder = "c:\temp"
- 它打開所有的Excel文件,然後使用Pearson's method在四個代碼來識別並替換某些字符串設定的文件夾上recursive Dir -module類型:
"c:\temp\xxx"
與
"d:\temp\yyy"
- 然後,代碼保存校準工作簿(但簡單地關閉未改變的工作簿)
- 所做的更改的摘要文件隨後被提供給用戶
一個編碼的VBE的特質之一是,使用一個字符串這裏變量失敗:
bFound = .Find(strOld, SL, SC, EL, EC, True, False, False)
我不得不硬編碼字符串替換,而不是
bFound = .Find("c:\temp\xxx", SL, SC, EL, EC, True, False, False)
Option Explicit
Public StrArray()
Public lngCnt As Long
Public Sub Main()
Dim objFSO As Object
Dim objFolder As Object
Dim WB As Workbook
Dim ws As Worksheet
Dim strStartFolder As String
'Setup Application for the user
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
'reset public variables
lngCnt = 0
ReDim StrArray(1 To 4, 1 To 1000)
strStartFolder = "c:\temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strStartFolder)
' Format output sheet
Set WB = Workbooks.Add(1)
Set ws = WB.Worksheets(1)
ws.[a1] = Now()
ws.[a2] = strStartFolder
ws.[a1:a3].HorizontalAlignment = xlLeft
ws.[A4:D4].Value = Array("Folder", "File", "Code Module", "line")
ws.Range([a1], [c4]).Font.Bold = True
ws.Rows(5).Select
ActiveWindow.FreezePanes = True
' Start the code to gather the files
ShowSubFolders objFolder, True
ShowSubFolders objFolder, False
If lngCnt > 0 Then
' Finalise output
With ws.Range(ws.[a5], ws.Cells(5 + lngCnt - 1, 4))
.Value2 = Application.Transpose(StrArray)
.Offset(-1, 0).Resize(Rows.Count - 3, 4).AutoFilter
.Offset(-4, 0).Resize(Rows.Count, 4).Columns.AutoFit
End With
ws.[a1].Activate
Else
MsgBox "No files found!", vbCritical
WB.Close False
End If
' tidy up
Set objFSO = Nothing
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.StatusBar = vbNullString
End With
End Sub
Sub ShowSubFolders(ByVal objFolder, bRootFolder As Boolean)
Dim colFolders As Object
Dim objSubfolder As Object
Dim WB As Workbook
Dim strOld As String
Dim strNew As String
Dim strFname As String
Dim VBProj As Object
Dim VBComp As Object
Dim CodeMod As Object
Dim bFound As Boolean
Dim bWBFound As Boolean
Dim SL As Long
Dim SC As Long
Dim EL As Long
Dim EC As Long
Dim S As String
strOld = "c:\temp\xxx"
strNew = "D:\temp\yyy"
Set colFolders = objFolder.SubFolders
Application.StatusBar = "Processing " & objFolder.Path
If bRootFolder Then
Set objSubfolder = objFolder
GoTo OneTimeRoot
End If
For Each objSubfolder In colFolders
'check to see if root directory files are to be processed
OneTimeRoot:
strFname = Dir(objSubfolder.Path & "\*.xls*")
Do While Len(strFname) > 0
Set WB = Workbooks.Open(objSubfolder.Path & "\" & strFname, False)
Set VBProj = WB.VBProject
For Each VBComp In VBProj.vbcomponents
Set CodeMod = VBComp.CodeModule
With CodeMod
SL = 1
EL = .CountOfLines
SC = 1
EC = 255
bFound = .Find("C:\test\xxx", SL, SC, EL, EC, True, False, False)
'bFound = .Find(strOld, SL, SC, EL, EC, True, False, False)
If bFound Then bWBFound = True
Do Until bFound = False
lngCnt = lngCnt + 1
If UBound(StrArray, 2) Mod 1000 = 0 Then ReDim Preserve StrArray(1 To 4, 1 To UBound(StrArray, 2) + 1000)
StrArray(1, lngCnt) = objSubfolder.Path
StrArray(2, lngCnt) = WB.Name
StrArray(3, lngCnt) = CodeMod.Name
StrArray(4, lngCnt) = SL
EL = .CountOfLines
SC = EC + 1
EC = 255
S = .Lines(SL, 1)
S = Replace(S, "C:\test\xxx", "D:\test\yyy")
.ReplaceLine SL, S
bFound = .Find("C:\test\xxx", SL, SC, EL, EC, True, False, False)
Loop
End With
Next
If bWBFound Then WB.Save
WB.Close False
strFname = Dir
Loop
If bRootFolder Then
bRootFolder = False
Exit Sub
End If
ShowSubFolders objSubfolder, False
Next
End Sub
我記得Excel有一個可擴展性庫,但我從來沒有用過它。快速搜索引導我閱讀本文[這裏](http://cpearson.com/excel/vbe.aspx),它可以幫助您開始。它基本上教你如何操作Excel文件的代碼模塊(宏是哪裏的)。祝你好運。 – ssarabando
@ssarabando很好的評論。我使用可擴展性庫運行直接替換。 – brettdj