我在一个文件夹中有超过500个excel文件,每个文件包含有错误的相同版本的VBA。通过注释掉几行VBA可以很容易地修复错误。 我能找到的只是如何批量替换工作表内的数据,而不是宏本身内部..
有没有办法将所有文件中的VBA内容从“sometext_inside_vba”批量替换为“'sometext_inside_vba”?
谢谢!
答案 0 :(得分:0)
这将检查单词Option Private Module
,如果找不到它们,它会将它们插入到处(定义为常量)
- Automatically add `Option Private Module` to all modules in VBA)如果切换几行,它就能满足您的需求。
Sub AddOptionPrivate()
Const UP_TO_LINE = 5
Const PRIVATE_MODULE = "Option Private Module"
Dim objXL As Object
Dim objPro As Object
Dim objComp As Variant
Dim strText As String
Set objXL = GetObject(, "Excel.Application")
Set objPro = objXL.ActiveWorkbook.VBProject
For Each objComp In objPro.VBComponents
If objComp.Type = 1 Then
strText = objComp.CodeModule.Lines(1, UP_TO_LINE)
If InStr(1, strText, PRIVATE_MODULE) = 0 Then
objComp.CodeModule.InsertLines 2, PRIVATE_MODULE
End If
End If
Next objComp
End Sub
一般情况下,必须阅读 - http://www.cpearson.com/excel/vbe.aspx从那里看SearchCodeModule
:
Sub SearchCodeModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim FindWhat As String
Dim SL As Long ' start line
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim Found As Boolean
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Module1")
Set CodeMod = VBComp.CodeModule
FindWhat = "findthis"
With CodeMod
SL = 1
EL = .CountOfLines
SC = 1
EC = 255
Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
EndLine:=EL, EndColumn:=EC, _
wholeword:=True, MatchCase:=False, patternsearch:=False)
Do Until Found = False
Debug.Print "Found at: Line: " & CStr(SL) & " Column: " & CStr(SC)
EL = .CountOfLines
SC = EC + 1
EC = 255
Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
EndLine:=EL, EndColumn:=EC, _
wholeword:=True, MatchCase:=False, patternsearch:=False)
Loop
End With
End Sub