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 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?
它怎么知道哪个子结点是它开始运行的主子结点?我可以把辅助子程序放在上面,代码从下面的子程序开始吗?
3 个解决方案
#1
30
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)你需要调用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 jey shortcut and only have to press those keys, on secondary subs i usually do it like Private sub CalculateA(...)
cause this way it doesnt appear in the macro list and it's easier to work
运行的第一个问题是你来决定,当你想要运行一个子你可以去宏列表中选择一个您想要运行,运行它,你也可以给它一个杰米快捷,只需要按那些键,等二次潜艇我通常做私人潜艇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格式。”
#2
3
These are really two questions.
这是两个问题。
The first one is answered here: Calling a Sub in VBA
第一个问题的答案是:在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.
对于第二个,protip: VBA中没有主子例程。忘记程序,通用的语言。VBA subs是“宏”——你可以通过按Alt+F8或在工作表中添加一个按钮来运行它们,并从自动生成的“ButtonX_Click”sub中调用你想要的sub。
#3
2
VBA subs are no macros. A VBA sub can be a macro, but it is not a must.
VBA下标不是宏。VBA可以是一个宏,但它不是必须的。
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.
术语“宏”只用于记录的用户操作。从这些操作中,代码被生成并存储在一个子程序中。直到,. .接下来,而. .做的事,和其他人。
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,那么您可以声明,总有一个主窗口或主窗体。如果您启动编译后的可执行文件“xxxxxxx .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中没有类似的东西,但是XLSM文件是由Excel启动的。您可以向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.
在基于对象的模型中,有对象,有事件和方法。方法是可以对对象执行的操作。事件是可以发生在对象身上的事情。对象可以包含其他对象,等等。您可以创建新的对象,如表或图表。
#1
30
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)你需要调用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 jey shortcut and only have to press those keys, on secondary subs i usually do it like Private sub CalculateA(...)
cause this way it doesnt appear in the macro list and it's easier to work
运行的第一个问题是你来决定,当你想要运行一个子你可以去宏列表中选择一个您想要运行,运行它,你也可以给它一个杰米快捷,只需要按那些键,等二次潜艇我通常做私人潜艇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格式。”
#2
3
These are really two questions.
这是两个问题。
The first one is answered here: Calling a Sub in VBA
第一个问题的答案是:在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.
对于第二个,protip: VBA中没有主子例程。忘记程序,通用的语言。VBA subs是“宏”——你可以通过按Alt+F8或在工作表中添加一个按钮来运行它们,并从自动生成的“ButtonX_Click”sub中调用你想要的sub。
#3
2
VBA subs are no macros. A VBA sub can be a macro, but it is not a must.
VBA下标不是宏。VBA可以是一个宏,但它不是必须的。
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.
术语“宏”只用于记录的用户操作。从这些操作中,代码被生成并存储在一个子程序中。直到,. .接下来,而. .做的事,和其他人。
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,那么您可以声明,总有一个主窗口或主窗体。如果您启动编译后的可执行文件“xxxxxxx .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中没有类似的东西,但是XLSM文件是由Excel启动的。您可以向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.
在基于对象的模型中,有对象,有事件和方法。方法是可以对对象执行的操作。事件是可以发生在对象身上的事情。对象可以包含其他对象,等等。您可以创建新的对象,如表或图表。