简单的重复或循环在 VBA 中调用 sub

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

Simple repeat or loop Call sub in VBA

excelvbaloopscall

提问by deafblind_TDK

I am wondering if there is a simpler way to "loop" a sub to do a number of times.

我想知道是否有一种更简单的方法来“循环”一个子程序来执行多次。

I would like to do the following:

我想做以下事情:

 Sub defined()
   'code here to be looped
  End sub

 Sub use()
 ... 'previous codes
 Call defined 'do defined once
 ... 'interlude codes
 Call defined(3) 'do defined action thrice
 ... 'interlude codes
 Call defined(2) 'do defined twice
 End Sub

Is there something like that exist? Or I must use Loop, something I still am a little confused on how to code when applying this method with a call sub.

有这样的东西存在吗?或者我必须使用 Loop,在使用 call sub 应用此方法时,我仍然对如何编码感到有些困惑。

Thank you for your time. edited for tags

感谢您的时间。为标签编辑

回答by David Zemens

You could do something like this:

你可以这样做:

Sub Defined(numCalls as Integer)
Dim i as Integer
If not numCalls > 0 Then Exit Sub
For i = 1 to numCalls

'The rest of your code


Next

End Sub

This will allow you to do:

这将允许您执行以下操作:

Call defined(3)

Etc.

等等。

回答by Patrick Honorez

for i = 1 to 3
     Defined   '("Call" is optional)
next i