VBA:使用多个参数在另一个工作表上调用子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27109127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:20:43  来源:igfitidea点击:

VBA: Calling a sub on another worksheet with multiple arguments

excelvbaexcel-vba

提问by MarkH

I've been struggling for a while to call a procedure (sub) with arguments that is located on another worksheet. I can call a procedure on another worksheet. I can call a procedure with arguments. But combining the two causes me headache's.

我一直在努力调用带有位于另一个工作表上的参数的过程(子)。我可以调用另一个工作表上的过程。我可以调用带有参数的过程。但是将两者结合起来会让我头疼。

Here's what I have now:

这是我现在所拥有的:

Private Sub Workbook_Open()
Call Sheet2.FillCombo("Mngt Dashboard", "ComboMonth")
'Sheet2.FillCombo"Operational Dashboard", "ComboMonth2"
End Sub

I tried both syntaxis for calling the procedure but both result in 'subscript out of range (9)'. Here's the procedure being called (located on sheet2):

我尝试了两种调用过程的语法,但都导致“下标超出范围 (9)”。这是被调用的过程(位于 sheet2 上):

Sub FillCombo(SheetName As String, ObjName As String)
Dim objCombo As Object
Set objCombo = ActiveWorkbook.Sheets(SheetName).OLEObjects(ObjName).Object
objCombo.Clear
objCombo.AddItem.....
...
End Sub

Is there anyone out there that can show me the light?

有没有人可以给我看灯?

Thanks in advance, Mark

提前致谢,马克

回答by Logan Spencer

On Line 2, try dropping the parenthesis:

在第 2 行,尝试去掉括号:

Sheet2.FillCombo "Mngt Dashboard", "ComboMonth"