如何在多个启用Excel的宏文件中更新vba代码?

时间:2017-12-05 08:55:49

标签: python excel excel-vba vba

我有一堆excel文件,所有这些文件都有相同的VBA代码。它们是从单个文件(模板文件)创建/克隆的,然后与其中的一些数据一起存储。所以我想更新所有excel文件中的VBA代码,包括模板文件(一个空的excel文件,里面没有数据)。有没有办法使用python脚本或其他东西一次性完成相同的操作。

2 个答案:

答案 0 :(得分:0)

如果文件已设置“信任访问VBA项目对象模型”选项,则可以在VBA中执行此操作。 http://www.cpearson.com/excel/vbe.aspx

答案 1 :(得分:0)

在可能的情况下,我尝试使用单独的.xlsx文件进行数据输入,并尝试使用单独的.xlsm程序文件对输入的数据进行操作,但是最近,当我需要不断更新代码中的代码时,我需要解决与此问题类似的问题模板,并且必须通过许多数据输入步骤来从这个空的数据模板多个文件中创建该代码。要求该文件可以在用户的​​PC上运行,而无需任何其他文件。

当模板被复制到一个单独的文件夹中时,我们使用了该解决方案,而工作簿中包含加载数据的工作表被复制到了该模板中,但是在这种情况下,某些工作表公式和带有已分配宏的按钮中的旧工作簿仍保留了链接。将数据从头输入到更新的模板中也不是一个好方法。

我决定用该程序解决问题,该程序将更新的模块从模板文件复制到所选文件中。我的代码如下:

Sub ControlSelectedWorkbookModulesUpdatingFromSourceWorkbook()

Dim sourceWorkbook As Workbook, destinationWorkbook As Workbook
Dim pathToSourceWorkbook As String
Dim fileDialog As Office.fileDialog, pathToDestinationWorkbook As String

Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
  .AllowMultiSelect = False
  .Title = "Please select the file."
  .Filters.Clear
  .Filters.Add "Excel", "*.xlsm"
  If .Show = True Then
    pathToDestinationWorkbook = fileDialog.SelectedItems(1)
  End If
End With

Application.ScreenUpdating = False

Set destinationWorkbook = Workbooks.Open(pathToDestinationWorkbook)

Call RemoveModules.RemoveAllVBA_ModulesFromDestinationWorkbook(destinationWorkbook)

pathToSourceWorkbook = "C:\Users\SourceFolder\SourceFileName.xlsm"
Set sourceWorkbook = Workbooks.Open(pathToSourceWorkbook)

Call CopyModules.CopyAllVBA_ModulesFromSourceWorkbookToDestinationWorkbook(sourceWorkbook, destinationWorkbook)

sourceWorkbook.Close SaveChanges:=False
destinationWorkbook.Close SaveChanges:=True

Application.ScreenUpdating = True

MsgBox "The end of program", vbInformation, ThisWorkbook.Name

End Sub

模块RemoveModules中的代码:

Sub RemoveAllVBA_ModulesFromDestinationWorkbook(destinationWorkbook As Workbook)

'This will remove all modules including ClassModules and UserForms but keep all 
'object modules (sheets, workbook).

Dim module As Object

On Error Resume Next
For Each module In destinationWorkbook.VBProject.VBComponents
    destinationWorkbook.VBProject.VBComponents.Remove module
Next
On Error GoTo 0

End Sub

模块CopyModules中的代码:

Sub CopyAllVBA_ModulesFromSourceWorkbookToDestinationWorkbook(sourceWorkbook As Workbook, destinationWorkbook As Workbook)

'This will copy from source workbook all modules except sheets, workbook.
'As direct copy is not allowed it is accomplished by 2 steps
'1 module from sourceWorkbook is exported into temporary file
'2 temporary file is imported into destinationWorkbook and then temporary file is deleted

Dim module As Object, pathToTemporaryFilesFolder As String, pathToTemporaryFile As String

pathToTemporaryFilesFolder = "C:\Users\OtherPartOfYourPath"

On Error Resume Next
For Each module In sourceWorkbook.VBProject.VBComponents
    If InStr(module.Name, "ThisWorkbook") = 0 And InStr(module.Name, "Sheet") = 0 Then
        pathToTemporaryFile = pathToTemporaryFilesFolder & "\" & module.Name & ".bas"
        module.Export (pathToTemporaryFile)
        destinationWorkbook.VBProject.VBComponents.Import (pathToTemporaryFile)
        Kill pathToTemporaryFile
    End If
Next
On Error GoTo 0

End Sub

要更新代码,只能设置PC Excel的用户-信任对VBA项目对象模型的访问,可以访问模板文件和此程序文件,但是可以将包含数据和更新代码的文件发送到其他用户,并将独立工作。