VBA 有一个子运行另一个子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14572989/
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-08 14:49:35  来源:igfitidea点击:

VBA have a sub run another sub

excelvbaexcel-vbaexcel-2003

提问by Stephan Daudt

how to run a substored in another worksheet's module?

如何运行sub存储在另一个工作表的模块中?

worksheet1

工作表1

sub endDay()
    'something here
end sub

worksheet2

工作表2

sub reCalc()
    'something here
end sub

I want recalcto be able to run on its own but I want to be able to press the button for "endDay", have it do its thing, and then preform "recalc" at the end instead of pressing one and then go to sheet2 to press the other.

我希望recalc能够自己运行,但我希望能够按下“endDay”的按钮,让它做它的事情,然后在最后预制“重新计算”而不是按下一个然后转到 sheet2按另一个。

Can someone give me a sample so I can have an idea where to begin?

有人可以给我一个样本,这样我就可以知道从哪里开始吗?

回答by Peter Albert

Take a look at the code names for the worksheets - you can see them in the tree view in the Visual Basic Editor. Usually they are named Sheet1, Sheet2, etc. (independent of the actual worksheet names, which are shown in brackets). Use this name in your code )instead of Worksheets("Sheet1")and you'll also get an autocomplete list - with your sub!

查看工作表的代码名称 - 您可以在 Visual Basic 编辑器的树视图中看到它们。通常它们被命名为Sheet1Sheet2等(独立于实际工作表名称,显示在括号中)。在您的代码中使用此名称 ) 而不是,Worksheets("Sheet1")您还将获得一个自动完成列表 - 与您的子!

Thus, this will do the job:

因此,这将完成这项工作:

Sheet1.reCalc

回答by Rick

sheets("worksheet2").reCalc

might be what you are after

可能是你所追求的

回答by Vityata

I know it is 2 years later, but today I was looking for the same thing. As far as the subs in the worksheets may be private, this is a good solution:

我知道是 2 年后,但今天我正在寻找同样的东西。至于工作表中的 subs 可能是私有的,这是一个很好的解决方案:

Sub Maina()
    Run "tbl_Input.btn_main_Click"
End Sub

tbl_Inputis the VBA name of the worksheet, not the caption.

tbl_Input是工作表的 VBA 名称,而不是标题。

回答by icebird76

Call Module1.reCalcshould work as well if the Sub is in Module1. It may be unnecessary to have 'Module1.' at all though.

Call Module1.reCalc如果 Sub 在 Module1 中也应该可以工作。可能没有必要拥有“模块 1”。尽管如此。