简体   繁体   English

Excel VBA调用模块

[英]Excel VBA Calling modules

So I have put together this code, but I am having trouble running it. 因此,我将这段代码放在一起,但是在运行它时遇到了麻烦。 I tried calling on the sub routines, but nothing was happening. 我尝试调用子例程,但没有任何反应。 I am stuck on how to call the modules I have created. 我被困在如何调用我创建的模块上。 I have also created a MainMenu() sub and I am wondering if that was maybe a mistake and I should have run my MainMenu from sheet1. 我还创建了MainMenu()子,我想知道这是否是一个错误,我应该从sheet1运行我的MainMenu。 Maybe someone has a better idea. 也许有人有更好的主意。

My original plan was to create a commandbutton where if clicked module1, which contains the MainMenu() would run. 我最初的计划是创建一个命令按钮,如果单击该按钮,将运行包含MainMenu()的module1。

Sub CommandButton1()

End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

Within module1 there is a loop that runs and calls the other three modules 在module1中,有一个循环运行并调用其他三个模块

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim row_sum As Integer

Sub Main()
'Finding the last row
row_sum = 0

'row check upto 5,000
For k = 1 To 5000
    ' if cells in k are blank then the row_check = 0, else row_check = 1
    If CStr(Cells(k, 1)) = "" Then row_check = 0 Else row_check = 1
    'adding the total of row_check to row_sum
    row_sum = row_sum + row_check
Next k
For j = 2 To row_sum
    Call Module2
    Call Module3
    Call Module4

Next j

End Sub

I can not figure out where I am going wrong exactly. 我无法弄清楚我到底要去哪里。 if anyone sees something I am missing please let me know. 如果有人看到我想念的东西,请告诉我。 I appreciate any help 感谢您的帮助

This is all the code we needed to see: 这是我们需要查看的所有代码:

 Call Module2 Call Module3 Call Module4 

=) =)

You don't "call" modules, modules are containers for procedures and functions. 您无需“调用”模块,模块是过程和功能的容器。 What you "call", is procedures and functions. 您所说的是过程和功能。 So if you have Sub DoSomething() in Module5 , you can invoke it like this: 因此,如果您在Module5具有Sub DoSomething()Module5可以这样调用它:

Module5.DoSomething

Or, if there's no DoSomething anywhere else: 或者,如果其他任何地方都没有DoSomething

DoSomething

Just like you would invoke VBA.Interaction.MsgBox like MsgBox "Hello!" 就像您像MsgBox "Hello!"那样调用VBA.Interaction.MsgBox一样 . Note that you don't need to have a Call keyword anywhere for it to work. 请注意,您无需在任何地方使用Call关键字即可使用。


Your Module2.SourceApprove procedure has parameters. 您的Module2.SourceApprove过程具有参数。 So you can't just do SourceApprove or Module2.SourceApprove , VBA will complain that a parameter is not optional . 所以你不能只做SourceApproveModule2.SourceApprove ,VBA会抱怨参数不是可选的

So you supply the parameters in a comma-separated list of values: 因此,您可以在参数中以逗号分隔的形式提供参数:

Module2.SourceApprove "first", "second", "third", 42

These could be variables, too. 这些也可以是变量。

Dim foo As Long
foo = 42

Module2.SourceApprove "first", "second", "third", foo

Consider using meaningful names for your parameters - that way IntelliSense will help you know what values to provide. 考虑为参数使用有意义的名称-IntelliSense可以帮助您知道要提供的值。 You can (should) also specify a type for the parameters, so that "first" can't be provided as a value for a parameter that requires a number. 您还可以(应该)为参数指定类型,以便不能将“ first”作为需要数字的参数的值提供。

Sub SourceApprove(ByVal i As Long, ByVal j As Long, ByVal k As Long, ByVal row_sum As Double)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM