Excel VBA 从具有多个输入、不同大小输出的另一个子程序调用子程序

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

Excel VBA calling sub from another sub with multiple inputs, outputs of different sizes

excelvbainputsubroutine

提问by user2421555

I would like to call a sub from another sub inside in the same module. The first sub would be my main code and there I would call the second subroutine. Second subroutine receives multiple inputs as integer, double, double arrays and double matrices. The size of the arrays and matrices are known and stored in an integer variable. The sub also returns several outputs. So, I would like to do something like this.

我想从同一模块中的另一个子程序调用子程序。第一个子程序将是我的主代码,我将在那里调用第二个子程序。第二个子程序接收多个输入作为整数、双精度、双精度数组和双精度矩阵。数组和矩阵的大小是已知的并存储在一个整数变量中。sub 还返回几个输出。所以,我想做这样的事情。

sub Main()
    Nc As integer
    Dim kij(1 To Nc, 1 To Nc), xi(1 to Nc), a1 As Double
    'I assign values to my variables from the excelsheet e.g. Nc=Cells(1,1) etc.

    CalculateA(Nc,kij, xi, a1, a)
    Cells(5,5)=a
end sub

sub CalculateA(Nc as integer,kij as matrix double, xi as array double, a as Double)
    a=0
    For i=1 To Nc
       For j=1 To Nc
          a = a + kij(i,j)*x(i)*x(j)
       Next j
    Next i
    a = a*a1
end sub

How does it know which sub is the main sub where it starts to run. Can I put the secondary sub on top and the code somehow starts from the bottom subroutine?

它如何知道哪个 sub 是它开始运行的主要 sub。我可以将辅助子程序放在顶部并且代码以某种方式从底部子程序开始吗?

回答by Newbie

To call a sub inside another sub you only need to do:

要在另一个子程序中调用子程序,您只需要执行以下操作:

Call Subname()

So where you have CalculateA(Nc,kij, xi, a1, a)you need to have call CalculateA(Nc,kij, xi, a1, a)

所以你有CalculateA(Nc,kij, xi, a1, a)你需要的地方call CalculateA(Nc,kij, xi, a1, a)

As the which runs first problem it's for you to decide, when you want to run a sub you can go to the macro list select the one you want to run and run it, you can also give it a key shortcut, therefore you will only have to press those keys to run it. Although, on secondary subs, I usually do it as Private sub CalculateA(...)cause this way it does not appear in the macro list and it's easier to work

至于哪个运行第一个问题是你自己决定的,当你想运行一个子时,你可以在宏列表中选择你要运行的那个并运行它,你也可以给它一个快捷键,因此你只会必须按这些键才能运行它。虽然,在二级潜艇上,我通常这样做是因为Private sub CalculateA(...)这样它不会出现在宏列表中并且更容易工作

Hope it helps, Bruno

希望它有所帮助,布鲁诺

PS: If you have any other question just ask, but this isn't a community where you ask for code, you come here with a question or a code that isn't running and ask for help, not like you did "It would be great if you could write it in the Excel VBA format."

PS:如果您有任何其他问题,尽管问,但这不是您索取代码的社区,您带着问题或未运行的代码来到这里寻求帮助,而不是像您那样“它会如果您能以 Excel VBA 格式编写它,那就太好了。”

回答by Carlos Vieira

These are really two questions.

这确实是两个问题。

The first one is answered here: Calling a Sub in VBA

第一个在这里回答:Calling a Sub in VBA

To the second one, protip: there is no main subroutine in VBA. Forget procedural, general-purpose languages. VBA subs are "macros" - you can run them by hitting Alt+F8 or by adding a button to your worksheet and calling up the sub you want from the automatically generated "ButtonX_Click" sub.

对于第二个,提示:VBA 中没有主子程序。忘记程序化的通用语言。VBA 子程序是“宏” - 您可以通过按 Alt+F8 或通过向工作表添加按钮并从自动生成的“ButtonX_Click”子程序中调用您想要的子程序来运行它们。

回答by Carlos Vieira

VBA subs are no macros. A VBA sub can be a macro, but it is not a must.

VBA 子程序不是宏。VBA sub 可以是宏,但不是必须的。

The term "macro" is only used for recorded user actions. from these actions a code is generated and stored in a sub. This code is simple and do not provide powerful structures like loops, for example Do .. until, for .. next, while.. do, and others.

术语“宏”仅用于记录的用户操作。从这些操作中生成一个代码并存储在子程序中。这段代码很简单,没有提供像循环这样的强大结构,例如 Do .. until、for .. next、while.. do 等。

The more elegant way is, to design and write your own VBA code without using the macro features!

更优雅的方法是,在不使用宏功能的情况下设计和编写自己的 VBA 代码!

VBA is a object based and event oriented language. Subs, or bette call it "sub routines", are started by dedicated events. The event can be the pressing of a button or the opening of a workbook and many many other very specific events.

VBA 是一种基于对象和面向事件的语言。Subs,或 bette 称之为“子程序”,由专用事件启动。事件可以是按下按钮或打开工作簿以及许多其他非常具体的事件。

If you focus to VB6 and not to VBA, then you can state, that there is always a main-window or main form. This form is started if you start the compiled executable "xxxx.exe".

如果您专注于 VB6 而不是 VBA,那么您可以声明,总有一个主窗口或主窗体。如果您启动已编译的可执行文件“xxxx.exe”,则会启动此表单。

In VBA you have nothing like this, but you have a XLSM file wich is started by Excel. You can attach some code to the Workbook_Open event. This event is generated, if you open your desired excel file which is called a workbook. Inside the workbook you have worksheets.

在 VBA 中,您没有这样的东西,但是您有一个由 Excel 启动的 XLSM 文件。您可以将一些代码附加到 Workbook_Open 事件。如果您打开称为工作簿的所需 Excel 文件,则会生成此事件。在工作簿中,您有工作表。

It is useful to get more familiar with the so called object model of excel. The workbook has several events and methods. Also the worksheet has several events and methods.

更熟悉所谓的 excel 对象模型很有用。工作簿有几个事件和方法。工作表还有几个事件和方法。

In the object based model you have objects, that have events and methods. methods are action you can do with a object. events are things that can happen to an object. An objects can contain another objects, and so on. You can create new objects, like sheets or charts.

在基于对象的模型中,您拥有具有事件和方法的对象。方法是您可以对对象执行的操作。事件是可能发生在对象上的事情。一个对象可以包含另一个对象,依此类推。您可以创建新对象,如工作表或图表。