你好,我需要Visual Basic的帮助, 我在许多excel文档中有一些VBA错误,所以我使用的代码将运行在包含excel文档的文件需要修复,我的问题,我需要找到并替换多行VBA代码,我的代码将找到只有单行并替换它,我的第二个问题,如果excel文档包含很多工作表我会得到一个错误,任何想法如何解决这两个问题?
Dim wb As Workbook
Dim strPath As String, strfile As String
Dim strToReplaceWith As String, strToReplace As String
Dim i As Long, j As Long
Dim VBE As Object
strPath = TextBox1.Text & "\"
strfile = Dir(strPath)
While strfile <> ""
Set wb = Workbooks.Open(strPath & strfile)
Set VBE = ActiveWorkbook.VBProject
If VBE.VBComponents.Item(1).Properties("HasPassword").Value = False Then
If VBE.VBComponents.Count > 0 Then
For i = 1 To VBE.VBComponents.Count
VBE.VBComponents.Item(i).Activate
If VBE.VBE.CodePanes.Item(i).CodeModule.CountOfLines > 0 Then
For j = 1 To VBE.VBE.CodePanes.Item(i).CodeModule.CountOfLines
If InStr(1, VBE.VBE.CodePanes.Item(i).CodeModule.Lines(j, 1), TextBox2.Text, vbTextCompare) Then
strToReplace = VBE.VBE.CodePanes.Item(i).CodeModule.Lines(j, 1)
strToReplaceWith = Replace(strToReplace, TextBox2.Text, TextBox3.Text, 1, 1, vbTextCompare)
VBE.VBE.CodePanes.Item(i).CodeModule.ReplaceLine j, strToReplaceWith
End If
Next
End If
Next i
End If
End If
wb.Close True
strfile = Dir
Wend