I have merged the workbook data as per the below code:
我按照以下代码合并了工作簿数据:
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
Now the problem is that, each individual workbook has a button with macro and some code on button click which work fine in individual file but in merged file when I click on the button then it opens that original file to run the code on button click, it is not using the code which is copied in merged file.
Let me know how can I make sure that the merged file doesn't depend on original file for running the button click code and should be independent.
现在的问题是,每个工作簿与宏观和一些代码有一个按钮按钮点击单个文件中工作良好但在合并文件当我点击按钮,然后打开原始文件上运行的代码按钮点击,它不是使用复制的代码合并文件。让我知道如何确保合并的文件不依赖于原始文件来运行按钮点击代码,并且应该是独立的。
Individual button click code as asked in comment:
如评论中所要求的个别按钮点击代码:
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
2 个解决方案
#1
1
I tested the code and problem seems to be limited to Form Buttons only. Try Using ActiveX buttons
我测试了代码,问题似乎仅限于表单按钮。尝试使用ActiveX按钮
In The Developer Tab, In Controls Group, On clicking the Insert dropdown button, You will notice that there are 2 groupings, upper one with heading Form Controls and lower one having heading ActiveX Controls. Insert buttons from the ActiveX Controls.
在Developer选项卡中,在控件组中,单击Insert下拉按钮,您将注意到有两个组,上面的组有标题表单控件,下面的组有标题ActiveX控件。从ActiveX控件中插入按钮。
#2
0
If you want to run the macro specific to the merged file you are executing from, preface:
如果您想要运行特定于您正在执行的合并文件的宏,前言:
Call Thisworkbook.MacroName
Be sure you initialize to the proper page in the workbook before executing. You can also control that with Thisworkbook.
在执行之前,一定要在工作簿中初始化到适当的页面。您也可以使用这个工作簿来控制它。
Thisworkbook.Sheetname
That will keep your execution from running in the original books.
这将阻止你的执行在原来的书中运行。
#1
1
I tested the code and problem seems to be limited to Form Buttons only. Try Using ActiveX buttons
我测试了代码,问题似乎仅限于表单按钮。尝试使用ActiveX按钮
In The Developer Tab, In Controls Group, On clicking the Insert dropdown button, You will notice that there are 2 groupings, upper one with heading Form Controls and lower one having heading ActiveX Controls. Insert buttons from the ActiveX Controls.
在Developer选项卡中,在控件组中,单击Insert下拉按钮,您将注意到有两个组,上面的组有标题表单控件,下面的组有标题ActiveX控件。从ActiveX控件中插入按钮。
#2
0
If you want to run the macro specific to the merged file you are executing from, preface:
如果您想要运行特定于您正在执行的合并文件的宏,前言:
Call Thisworkbook.MacroName
Be sure you initialize to the proper page in the workbook before executing. You can also control that with Thisworkbook.
在执行之前,一定要在工作簿中初始化到适当的页面。您也可以使用这个工作簿来控制它。
Thisworkbook.Sheetname
That will keep your execution from running in the original books.
这将阻止你的执行在原来的书中运行。