I am trying to write a macro that would "click" a command button that is in another workbook.. Would that be possible? Without changing any of the code within that other workbook?
Thanks a lot!
我正在尝试编写一个宏,它将“单击”另一个工作簿中的命令按钮。这可能吗?不改变其他工作簿中的任何代码?非常感谢!
7 个解决方案
#1
4
For an ActiveX button in another workbook:
对于另一个工作簿中的ActiveX按钮:
Workbooks("OtherBook").Worksheets("Sheet1").CommandButton1.Value = True
For an MSForms button in another workbook:
对于另一个工作簿中的MSForms按钮:
Application.Run Workbooks("OtherBook").Worksheets("Sheet1").Shapes("Button 1").OnAction
#2
1
You can use Application.Run
for that:
您可以使用Application.Run:
Run "OtherWorkbook.xls!MyOtherMacro"
#3
1
Instead of trying to programatically click the button, it is possible to run the macro linked to the button directly from your code.
而不是尝试以编程方式单击按钮,可以直接从代码运行链接到按钮的宏。
First you need to find the name of the macro that is run when the button is clicked.
首先,您需要找到单击按钮时运行的宏的名称。
To do this, open the workbook that contains the command button.
为此,请打开包含命令按钮的工作簿。
Right click on the command button and select 'Assign macro'
右键单击命令按钮并选择“分配宏”
The 'Assign macro' dialog will be displayed.
将显示“分配宏”对话框。
Make a note of the full name in the 'Macro name' box at the top of the dialog.
在对话框顶部的“宏名称”框中记下全名。
Click on the OK button.
单击“确定”按钮。
Your code in the workbook that needs to call the code should be as follows.
您需要调用代码的工作簿中的代码应如下所示。
Sub Run_Macro()
Workbooks.Open Filename:="C:\Book1.xls"
'Open the workbook containing the command button
'Change the path and filename as required
Application.Run "Book1.xls!Macro1"
'Run the macro
'Change the filename and macro name as required
'If the macro is attached to a worksheet rather than a module, the code would be
'Application.Run "Book1.xls!Sheet1.Macro1"
End Sub
#4
0
You sure you mean workbook and not sheet? Anyways if you "want to loop through all workbooks in a folder and perform an operation on each of them"
你确定你的意思是工作簿而不是工作表?无论如何,如果你“想要遍历文件夹中的所有工作簿并对每个工作簿执行操作”
If you want to access another sheet it's done like this:
如果你想访问另一张表,它就像这样:
Worksheets("MySheet").YourMethod()
#5
0
I had a similar problem as above and it took a while to figure out but this is all I had to do.
我遇到了类似的问题,需要一段时间来弄明白,但这就是我必须要做的。
On Sheet1 I created a button with the following:
在Sheet1上,我创建了一个包含以下内容的按钮:
Private Sub cmdRefreshAll_Click()
Dim SheetName As String
SheetName = "Mon"
Worksheets(SheetName).Activate
ActiveSheet.cmdRefresh_Click
SheetName = "Tue"
Worksheets(SheetName).Activate
ActiveSheet.cmdRefresh_Click
' "I repeated the above code to loop through a worksheet for every day of the week"
End Sub
Then the importing bit to get it to work you need to go to the code on the other worksheets and change it from Private to Public.
然后导入位使其工作,您需要转到其他工作表上的代码并将其从Private更改为Public。
So far no bugs have popped up.
到目前为止,没有出现任何漏洞。
#6
0
NOPE:
This is EASY to do:
这很容易做到:
-
In the worksheet ABC where you have the Private Sub xyz_Click(), add a new public subroutine:
在您拥有Private Sub xyz_Click()的工作表ABC中,添加一个新的公共子例程:
Public Sub ForceClickOnBouttonXYZ() Call xyz_Click End Sub
Public Sub ForceClickOnBouttonXYZ()调用xyz_Click End Sub
-
In your other worksheet or module, add the code:
在您的其他工作表或模块中,添加代码:
Sheets("ABC").Activate Call Sheets("ABC").ForceClickOnBouttonXYZ
THAT IS IT!!! If you do not want the screen to flicker or show any activity, set the Application.ScreenUpdating = False before you call the Sheets("ABC").ForceClickOnBouttonXYZ routine and then set Application.ScreenUpdating = True right after it.
这就对了!!!如果您不希望屏幕闪烁或显示任何活动,请在调用表格(“ABC”)之前设置Application.ScreenUpdating = False.ForceClickOnBouttonXYZ例程,然后在它之后设置Application.ScreenUpdating = True。
#7
-1
There's not a clean way to do this through code, since the button's click event would typically be a private method of the other workbook.
通过代码执行此操作并不是一种干净的方法,因为按钮的单击事件通常是其他工作簿的私有方法。
However, you can automate the click through VBA code by opening the workbook, finding the control you want, and then activating it and sending it a space character (equivalent to pressing the button).
但是,您可以通过打开工作簿,找到所需的控件,然后激活它并向其发送空格字符(相当于按下按钮)来自动点击VBA代码。
This is almost certainly a terrible idea, and it will probably bring you and your offspring nothing but misery. I'd urge you to find a better solution, but in the meantime, this seems to work...
这几乎肯定是一个可怕的想法,它可能会给你和你的后代带来痛苦。我敦促你找到一个更好的解决方案,但与此同时,这似乎有效......
Public Sub RunButton(workbookName As String, worksheetName As String, controlName As String)
Dim wkb As Workbook
Dim wks As Worksheet
Set wkb = Workbooks.Open(workbookName)
wkb.Activate
Dim obj As OLEObject
For Each wks In wkb.Worksheets
If wks.Name = worksheetName Then
wks.Activate
For Each obj In wks.OLEObjects
If (obj.Name = controlName) Then
obj.Activate
SendKeys (" ")
End If
Next obj
End If
Next wks
End Sub
#1
4
For an ActiveX button in another workbook:
对于另一个工作簿中的ActiveX按钮:
Workbooks("OtherBook").Worksheets("Sheet1").CommandButton1.Value = True
For an MSForms button in another workbook:
对于另一个工作簿中的MSForms按钮:
Application.Run Workbooks("OtherBook").Worksheets("Sheet1").Shapes("Button 1").OnAction
#2
1
You can use Application.Run
for that:
您可以使用Application.Run:
Run "OtherWorkbook.xls!MyOtherMacro"
#3
1
Instead of trying to programatically click the button, it is possible to run the macro linked to the button directly from your code.
而不是尝试以编程方式单击按钮,可以直接从代码运行链接到按钮的宏。
First you need to find the name of the macro that is run when the button is clicked.
首先,您需要找到单击按钮时运行的宏的名称。
To do this, open the workbook that contains the command button.
为此,请打开包含命令按钮的工作簿。
Right click on the command button and select 'Assign macro'
右键单击命令按钮并选择“分配宏”
The 'Assign macro' dialog will be displayed.
将显示“分配宏”对话框。
Make a note of the full name in the 'Macro name' box at the top of the dialog.
在对话框顶部的“宏名称”框中记下全名。
Click on the OK button.
单击“确定”按钮。
Your code in the workbook that needs to call the code should be as follows.
您需要调用代码的工作簿中的代码应如下所示。
Sub Run_Macro()
Workbooks.Open Filename:="C:\Book1.xls"
'Open the workbook containing the command button
'Change the path and filename as required
Application.Run "Book1.xls!Macro1"
'Run the macro
'Change the filename and macro name as required
'If the macro is attached to a worksheet rather than a module, the code would be
'Application.Run "Book1.xls!Sheet1.Macro1"
End Sub
#4
0
You sure you mean workbook and not sheet? Anyways if you "want to loop through all workbooks in a folder and perform an operation on each of them"
你确定你的意思是工作簿而不是工作表?无论如何,如果你“想要遍历文件夹中的所有工作簿并对每个工作簿执行操作”
If you want to access another sheet it's done like this:
如果你想访问另一张表,它就像这样:
Worksheets("MySheet").YourMethod()
#5
0
I had a similar problem as above and it took a while to figure out but this is all I had to do.
我遇到了类似的问题,需要一段时间来弄明白,但这就是我必须要做的。
On Sheet1 I created a button with the following:
在Sheet1上,我创建了一个包含以下内容的按钮:
Private Sub cmdRefreshAll_Click()
Dim SheetName As String
SheetName = "Mon"
Worksheets(SheetName).Activate
ActiveSheet.cmdRefresh_Click
SheetName = "Tue"
Worksheets(SheetName).Activate
ActiveSheet.cmdRefresh_Click
' "I repeated the above code to loop through a worksheet for every day of the week"
End Sub
Then the importing bit to get it to work you need to go to the code on the other worksheets and change it from Private to Public.
然后导入位使其工作,您需要转到其他工作表上的代码并将其从Private更改为Public。
So far no bugs have popped up.
到目前为止,没有出现任何漏洞。
#6
0
NOPE:
This is EASY to do:
这很容易做到:
-
In the worksheet ABC where you have the Private Sub xyz_Click(), add a new public subroutine:
在您拥有Private Sub xyz_Click()的工作表ABC中,添加一个新的公共子例程:
Public Sub ForceClickOnBouttonXYZ() Call xyz_Click End Sub
Public Sub ForceClickOnBouttonXYZ()调用xyz_Click End Sub
-
In your other worksheet or module, add the code:
在您的其他工作表或模块中,添加代码:
Sheets("ABC").Activate Call Sheets("ABC").ForceClickOnBouttonXYZ
THAT IS IT!!! If you do not want the screen to flicker or show any activity, set the Application.ScreenUpdating = False before you call the Sheets("ABC").ForceClickOnBouttonXYZ routine and then set Application.ScreenUpdating = True right after it.
这就对了!!!如果您不希望屏幕闪烁或显示任何活动,请在调用表格(“ABC”)之前设置Application.ScreenUpdating = False.ForceClickOnBouttonXYZ例程,然后在它之后设置Application.ScreenUpdating = True。
#7
-1
There's not a clean way to do this through code, since the button's click event would typically be a private method of the other workbook.
通过代码执行此操作并不是一种干净的方法,因为按钮的单击事件通常是其他工作簿的私有方法。
However, you can automate the click through VBA code by opening the workbook, finding the control you want, and then activating it and sending it a space character (equivalent to pressing the button).
但是,您可以通过打开工作簿,找到所需的控件,然后激活它并向其发送空格字符(相当于按下按钮)来自动点击VBA代码。
This is almost certainly a terrible idea, and it will probably bring you and your offspring nothing but misery. I'd urge you to find a better solution, but in the meantime, this seems to work...
这几乎肯定是一个可怕的想法,它可能会给你和你的后代带来痛苦。我敦促你找到一个更好的解决方案,但与此同时,这似乎有效......
Public Sub RunButton(workbookName As String, worksheetName As String, controlName As String)
Dim wkb As Workbook
Dim wks As Worksheet
Set wkb = Workbooks.Open(workbookName)
wkb.Activate
Dim obj As OLEObject
For Each wks In wkb.Worksheets
If wks.Name = worksheetName Then
wks.Activate
For Each obj In wks.OLEObjects
If (obj.Name = controlName) Then
obj.Activate
SendKeys (" ")
End If
Next obj
End If
Next wks
End Sub