Excel VBA调用子从另一个子与多个输入,不同大小的输出 [英] Excel VBA calling sub from another sub with multiple inputs, outputs of different sizes

查看:225
本文介绍了Excel VBA调用子从另一个子与多个输入,不同大小的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  sub Main()
Nc作为整数
Dim kij(1到Nc,1到Nc),xi(1到Nc),a1 As Double
'我从excelsheet将值分配给我的变量,例如Nc =细胞(1,1)等

计算A(Nc,kij,xi,a1,a)
细胞(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
对于j = 1到Nc
a = a + kij(i,j)* x(i)* x(j)
下一个j
下一个i
a = a * a1
end sub

它如何知道哪个子是主开始运行的主子。可以把辅助子放在上面,代码从底部的子程序开始吗?

解决方案

你只需要做:

  Call Subname()
CalculateA(Nc,kij,xi,a1,a)你需要有$ code>调用CalculateA(Nc,kij,xi,a1,a)



由于运行第一个问题是由您决定的你想运行一个sub你可以去宏列表选择一个你想要运行它运行它,你也可以给它一个快捷键,只需要按这些键,在辅助subs我通常会像 Private sub CalculateA(...)因为这样,它不会出现在宏列表中,它更容易工作



希望它有帮助,
布鲁诺



PS:如果你还有其他问题只是问,但这不是一个社区,你要求代码,你来这里有一个问题或一个代码这不是运行并且要求帮助,而不是像你做的那样如果你可以用Excel VBA格式编写它将会很好。


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?

解决方案

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)

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

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."

这篇关于Excel VBA调用子从另一个子与多个输入,不同大小的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆