|
本帖最后由 crazy0qwer 于 2016-10-31 23:34 编辑
貌似不用?(1、勾选 "信任对 VBA 工程对象模型的访问" , 开启这个功能后可能增加中宏病毒风险。)
2、如需批量更新,请先自行加入对文件的循环。
代码更新.rar
(17.37 KB, 下载次数: 37)
- Sub Update()
- Dim F
- Dim Cl As Object
- Dim I As Long, N As Long
- Dim Ar() As String
- On Error Resume Next
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Application.EnableEvents = False '防止open close 等事件触发
- '*****************************************************************
- '获取新代码
- ReDim Ar(ThisWorkbook.VBProject.VBComponents.Count, 2)
- For Each Cl In ThisWorkbook.VBProject.VBComponents
- If m.Name <> "Update" Then '不读取本模块代码
- Ar(N, 1) = Cl.Name
- Ar(N, 2) = Cl.CODEMODULE.Lines(1, Cl.CODEMODULE.COUNTOFLINES)
- N = N + 1
- End If
- Next
- '*****************************************************************
- '获取待更新文件
- F = Application.GetOpenFilename("Excel(*.xls*),*.xls*", , "选择要更新的文件", , False)
- If F = False Then Exit Sub
- '*****************************************************************
- '打开待更新文件,删除旧代码,写入新代码
- With Workbooks.Open(F)
- With .VBProject
- For I = 0 To N - 1
- Set Cl = Nothing
- Set Cl = .VBComponents(Ar(I, 1))
- If Not Cl Is Nothing Then
- With Cl.CODEMODULE
- .DELETELINES 1, .COUNTOFLINES
- .ADDFROMSTRING Ar(I, 2)
- End With
- End If
- Next
- End With
- '*****************************************************************
- ' XLSX 文件需要另存为启用宏的工作表 XLSM 格式
- If UCase(Right(F, 4)) = "XLSX" Then
- .SaveAs Left(F, Len(F) - 5), 52
- 'Kill F '删除 XLSX 格式原文件
- Else
- .Save
- End If
- .Close True
- End With
- Application.EnableEvents = True
- End Sub
复制代码
|
评分
-
2
查看全部评分
-
|