第一次尝试用代码来修改代码,效果拔群,现在把代码分享给大家。
1.使用vba代码批量查找并替换vba代码
Sub CommandButton1_Click()On Error Resume NextDim fd As FileDialog, itDim fso As ObjectDim file_name As StringDim strFolder As StringDim wb As WorkbookDim i As IntegerDim codeDim str1 As StringDim stra As StringDim codestr As StringDim num As LongDim newstr As Stringstr1 = "original"stra = "new"Set fd = application.FileDialog(msoFileDialogFilePicker)Set fso = CreateObject("Scripting.FileSystemObject")With fd.AllowMultiSelect = TrueIf .Show = -1 ThenFor Each it In .SelectedItemsfile_name = fso.GetFileName(it)strFolder = itapplication.ScreenUpdating = FalseSet wb = Workbooks.Open(strFolder)For Each code In wb.VBProject.VBComponentsDebug.Print code.Namenum = code.codemodule.CountOfLinesFor i = 1 To numcodestr = code.codemodule.Lines(i, 1)If InStr(codestr, str1) ThenDebug.Print codestrnewstr = replace(codestr, str1, stra, 1)code.codemodule.DeleteLines icode.codemodule.InsertLines i, newstrEnd IfNext iNextwb.Close Trueapplication.ScreenUpdating = TrueNextEnd IfEnd WithMsgBox "附件处理成功"End Sub
2.使用vba代码批量添加vba代码
Sub CommandButton1_Click()On Error Resume NextDim fd As FileDialog, itDim fso As ObjectDim file_name As StringDim new_dir As StringDim strFolder As StringDim wb As WorkbookDim d As ObjectSet d = CreateObject("scripting.dictionary")Dim i As IntegerDim j As IntegerDim k As IntegerSet fd = application.FileDialog(msoFileDialogFilePicker)Set fso = CreateObject("Scripting.FileSystemObject")j = ThisWorkbook.Sheets("filename").Cells(Rows.Count, 2).End(xlUp).Row + 1k = ThisWorkbook.Sheets("done").Cells(Rows.Count, 2).End(xlUp).Row + 1With fd.AllowMultiSelect = TrueIf .Show = -1 ThenFor Each it In .SelectedItemsfile_name = fso.GetFileName(it)d("file_name") = d("file_name") & file_name & ","strFolder = itd("strFolder") = d("strFolder") & strFolder & ","application.ScreenUpdating = FalseSet wb = Workbooks.Open(strFolder)i = wb.VBProject.VBComponents("Thisworkbook").codemodule.ProcStartLine("Workbook_Open", 0)Debug.Print iIf i = 0 Thenwb.VBProject.VBComponents("Thisworkbook").codemodule.AddFromString _"Private Sub Workbook_Open()" & Chr(10) & _"End Sub"'在此处添加代码段ThisWorkbook.Sheets("done").Range("b" & k).Value = strFolderk = k + 1Else:ThisWorkbook.Sheets("filename").Range("b" & j).Value = strFolderj = j + 1End Ifwb.Close Trueapplication.ScreenUpdating = TrueNextEnd IfEnd WithMe.TextBox1.Text = d("file_name")MsgBox "附件处理成功"End Sub
3.每次打开excel自动更新vba代码中的模块
Private Sub Workbook_Open()Dim s As StringDim arrDim brrDim i As IntegerOn Error Resume Nextarr = Split("aa.bas,bb.bas,cc.cls,ee.bas,ff.bas", ",")'将模块名写入数组arrFor i = 0 To UBound(arr)brr = Split(arr(i), ".")'取模块名字s = ThisWorkbook.VBProject.VBComponents(brr(0)).NameIf Err ThenThisWorkbook.application.VBE.ActiveVBProject.VBComponents.Import "module\" & arr(i)Debug.Print "成功添加" & brr(0) & "模块"'模块存放地址Else:ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents(brr(0))ThisWorkbook.application.VBE.ActiveVBProject.VBComponents.Import "module\" & arr(i)Debug.Print "成功替换" & brr(0) & "模块"End IfNext iDebug.Print "导入成功!"End Sub
文章转载自苦方米唐,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




