vba 从另一个子调用类型变量

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

Calling type variables from another sub

vbaexcel-vbaglobal-variablessubroutineexcel

提问by marillion

Hi I have a series of subroutines as follows:

嗨,我有一系列子程序如下:

  1. DataCollection() : Collects data from the spreadsheet and writes it to custom type variables.

  2. NewSub() : Does something else, but not relevant to the question.

  1. DataCollection() :从电子表格中收集数据并将其写入自定义类型变量。

  2. NewSub() :做其他事情,但与问题无关。

I would like to keep the same variables previously declared, and having values assigned in the second sub. I think I have to make them global variables somehow, but could not work it out so far, whatever I do I get the variable not defined error. My code is as follows:

我想保留先前声明的相同变量,并在第二个子项中分配值。我想我必须以某种方式使它们成为全局变量,但到目前为止无法解决,无论我做什么,我都会得到变量未定义错误。我的代码如下:

Option Explicit

Public Type Trucks
    NumberOfAxles As Integer
    AxleWeights(15) As Double
End Type

Public Sub DataCollection()

Dim NumberOfTrucks As Integer
Truck(10) As Trucks
Dim i, j, k As Integer

'Determine Number of Trucks
NumberOfTrucks = Cells(6, 8)

'Populate Truck Arrays (Trucks 1 to 5)

k = 0
For i = 1 To 5
    Truck(i).NumberOfAxles = Cells(9, 4 + 4 * k)
    k = k + 1
Next i

k = 0
For i = 1 To 5
    For j = 1 To Truck(i).NumberOfAxles
        Truck(i).AxleWeights(j) = Cells(31 + j, 3 + 4 * k)
    Next j
    k = k + 1
Next i

End Sub

Public Sub NewSub()

For i = 1 To Truck(10).NumberOfAxles
    Cells(27 + i, 22) = Truck(10).AxleWeights(i)
Next i

End Sub

Any ideas would be most welcome! Thanks!

任何想法将是最受欢迎的!谢谢!

回答by Dick Kusleika

Keep your variables in as limited a scope as possible.

将变量保持在尽可能有限的范围内。

If you call NewSub from DataCollection, then make Trucks() local to DataCollection and pass it as an argument to NewSub.

如果您从 DataCollection 调用 NewSub,则将 Trucks() 设为 DataCollection 的本地并将其作为参数传递给 NewSub。

If you don't call one from the other but they are in the same module declare Trucks() as a module-level variable. To do that use the Private keyword and make the declaration at the top of the module outside of any procedures.

如果您不从另一个调用一个,但它们在同一个模块中,则将 Trucks() 声明为模块级变量。为此,请使用 Private 关键字并在任何过程之外的模块顶部进行声明。

Finally, if NewSub is in a different module, you need to declare a global variable. Use the Public keyword and declare it in it's own module called MGlobals. Why it's own module? It's good practice to limit your use of global variables and declare them all in the same place so you can manage them more effectively. (That means move your public Type to MGlobals too.)

最后,如果 NewSub 在不同的模块中,则需要声明一个全局变量。使用 Public 关键字并在它自己的名为 MGlobals 的模块中声明它。为什么它是自己的模块?最好限制对全局变量的使用并将它们全部声明在同一位置,以便更有效地管理它们。(这意味着也将您的公共类型移动到 MGlobals。)

OK, having said all that, stop using Types now. At some point in your project, you're going to want some function that is beyond what Type can do for you. I know you don't think so, but it will happen. So you'll create a function that does it and it will become an unmanageable mess. So make a Truck class and a Trucks class. The Truck class will contain the two properties. The Trucks class will contain a private collection object that holds all the Truck instances. The only global variable you'll need is gclsTrucks. As long as that is in scope, all of your Truck instances. All of your heavy lifting should be on in the Truck class. A little extra work right now will save you big.

好吧,说了这么多,现在停止使用类型。在您的项目中的某个时刻,您将需要一些 Type 无法为您完成的功能。我知道你不这么认为,但它会发生。因此,您将创建一个执行此操作的函数,它会变得一团糟。所以制作一个卡车类和一个卡车类。Truck 类将包含这两个属性。Trucks 类将包含一个私有集合对象,该对象包含所有 Truck 实例。您需要的唯一全局变量是 gclsTrucks。只要在范围内,您的所有 Truck 实例。你所有的繁重工作都应该在卡车课上。现在做一些额外的工作将为您节省很多。

回答by fabiopagoti

You can use global variables like follows.

您可以使用如下的全局变量。

Dim global_var As Integer
'

Sub doA()
global_var = global_var + 1
Debug.Print global_var

End Sub

Sub doB()
global_var = global_var + 10
Debug.Print global_var
End Sub

Sub main()
doA
doB
doA
End Sub

You declare your variable in

你声明你的变量

Truck(10) As Trucks

and not on

而不是

Public Type Trucks
    NumberOfAxles As Integer
    AxleWeights(15) As Double
End Type

In other words, just move the "Dim" to outside the routine.

换句话说,只需将“Dim”移到例程之外。