新的组合工作簿中的macros链接更新从多个Excel工作簿

我按照以下代码合并了工作簿数据:

Sub Merge2MultiSheets() Dim wbDst As Workbook Dim wbSrc As Workbook Dim wsSrc As Worksheet Dim MyPath As String Dim strFilename As String Dim CurrentRow As Integer Dim CurrentColumn As Integer Dim Index As Integer Application.DisplayAlerts = False Application.EnableEvents = False Application.ScreenUpdating = False MyPath = "D:\Excels" ' change to suit CurrentRow = 1 CurrentColumn = 1 Index = 0 Set wbSource = ActiveWorkbook strFilename = wbSource.Worksheets("Test").Cells(CurrentRow, Index + 1) Set wbDst = Workbooks.Add(xlWBATWorksheet) 'strFilename = Dir(MyPath & "\*.xlsx", vbNormal) If Len(strFilename) = 0 Then Exit Sub Do Until strFilename = "" Set wbSrc = Workbooks.Open(fileName:=MyPath & "\" & strFilename) Set wsSrc = wbSrc.Worksheets(1) wsSrc.Copy after:=wbDst.Worksheets(wbDst.Worksheets.Count) Set wsSrc = wbSrc.Worksheets(2) wsSrc.Copy after:=wbDst.Worksheets(wbDst.Worksheets.Count) wbSrc.Close False Index = Index + 1 strFilename = wbSource.Worksheets("Test").Cells(CurrentRow, Index + 1) Loop wbDst.Worksheets(1).Delete Application.DisplayAlerts = True Application.EnableEvents = True Application.ScreenUpdating = True End Sub 

现在的问题是,每个单独的工作簿都有一个button,其中包含macros和button点击的一些代码,在单独的文件中工作正常,但在合并的文件中,当我点击button然后打开原始文件运行button点击代码,它不使用在合并文件中复制的代码。

让我知道如何确保合并文件不依赖原始文件运行button点击代码,应该是独立的。

个别button点击代码,如评论中所述:

 Sub Calculate_Click() Dim sum As Integer Dim mult As Integer Dim input1 As Integer Dim input2 As Integer input1 = Worksheets("test1_input").Cells(1, 2) input2 = Worksheets("test1_input").Cells(2, 2) sum = input1 + input2 mult = input1 * input2 Worksheets("test1_output").Cells(1, 2) = sum Worksheets("test1_output").Cells(2, 2) = mult End Sub 

我testing了代码,问题似乎只限于表单button。 尝试使用ActiveXbutton

在“ 开发人员”选项卡的“ 控件组”中,单击“插入”下拉列表button,您将注意到有2个分组,上面的分组具有标题forms控件 ,下面的分组具有标题ActiveX控件 。 从ActiveX控件插入button。

如果要运行特定于您正在执行的合并文件的macros,请input:

 Call Thisworkbook.MacroName 

确保在执行之前初始化到工作簿中的正确页面。 你也可以使用Thisworkbook来控制它。

 Thisworkbook.Sheetname 

这将保持您的执行从原来的书籍中运行。