简体   繁体   English

在vba的单个子/函数中可以使用多少个变量?

[英]How many variables can be used in a single sub/function of vba?

I have built a simple function but it needs around 11-12 variable that have the same integer type. 我已经构建了一个简单的函数,但它需要大约11-12个具有相同整数类型的变量。 Every time I tried to used with more than 10 variables, the first one often gets wrong while the others are correctly calculated. 每次我尝试使用超过10个变量时,第一个变量经常出错,而其他变量被正确计算。 If I reduce the number of variables down to 10, they they are all correct. 如果我将变量数减少到10,那么它们都是正确的。 Does it mean hat vba can only store 10 variables with the same type at a time? 这是否意味着hat vba一次只能存储10个相同类型的变量? Or it is my computer' limit :)) 或者这是我的电脑'限制:))

This is my code, btb return the wrong value while the others is correct, but if I delete variable "a" and all the code related to it, then btb return correctly 这是我的代码,btb返回错误的值,而其他人是正确的,但如果我删除变量“a”和所有与之相关的代码,那么btb正确返回

Sub regionCount()
Dim btb, dbb, hn, ntb, dnb, tnbBS, tnbNS, hcm1, hcm2, hcm3, a As Integer

btb = dbb = hn = dnb = tnbBS = tnbNS = hcm1 = hcm2 = hcm3 = a = 0

For Each mycell In Worksheets(1).Range("E2:E382")
    If mycell.Value = "Khu vuc Bac Trung Bo" Then
        btb = btb + 1
    ElseIf mycell.Value = "Khu vuc Dong Bac Bo" Then
        dbb = dbb + 1
    ElseIf mycell.Value = "Khu vuc Ha Noi" Then
        hn = hn + 1
    ElseIf mycell.Value = "Khu vuc Nam Trung Bo" Then
        ntb = ntb + 1
    ElseIf mycell.Value = "Khu vuc Dong Nam Bo" Then
        dnb = dnb + 1
    ElseIf mycell.Value = "Khu vuc Tay Nam Bo  - Bac song hau" Then
        tnbBS = tnbBS + 1
    ElseIf mycell.Value = "Khu vuc Tay Nam Bo  - Nam song hau" Then
        tnbNS = tnbNS + 1
    ElseIf mycell.Value = "Khu vuc TPHCM_1" Then
        hcm1 = hcm1 + 1
    ElseIf mycell.Value = "Khu vuc TPHCM_2" Then
        hcm2 = hcm2 + 1
    ElseIf mycell.Value = "Khu vuc TPHCM_3" Then
        hcm3 = hcm3 + 1
    ElseIf mycell.Value = "-- None --" Then
        a = a + 1
    End If
Next mycell

Range("C5").Value = btb
Range("d5").Value = dbb
Range("e5").Value = hn
Range("f5").Value = ntb
Range("g5").Value = dnb
Range("h5").Value = tnbBS
Range("i5").Value = tnbNS
Range("j5").Value = hcm1
Range("k5").Value = hcm2
Range("l5").Value = hcm3
Range("m5").Value = a

End Sub 结束子

Your statement 你的陈述

btb = dbb = hn = dnb = tnbBS = tnbNS = hcm1 = hcm2 = hcm3 = a = 0

should be 应该

btb = 0
dbb = 0
hn = 0
dnb = 0
tnbBS = 0
tnbNS = 0
hcm1 = 0
hcm2 = 0
hcm3 = 0
a = 0

Consider the statement 考虑一下这句话

a = b = c

That is interpreted as 这被解释为

a = (b = c)

where (b = c) is a logical expression. 其中(b = c)是一个逻辑表达式。 So, if b is the same value as c (which it will be if the variables have all been left to the default initialisation of 0 ) then a will be set to True (ie -1 ) 因此,如果bc值相同(如果变量全部保留为默认初始化0那么将为a ),则a将设置为True (即-1

The way you had your statement laid out would have set the value on the very left to be -1 (if you had an odd number of variables in the line) or to 0 (if you had an even number). 你的语句布局方式会将最左边的值设置为-1(如果行中有奇数个变量)或者设置为0(如果你有一个偶数)。


It should also be noted that 还应该指出的是

Dim btb, dbb, hn, ntb, dnb, tnbBS, tnbNS, hcm1, hcm2, hcm3, a As Integer

is equivalent to 相当于

Dim btb As Variant, dbb As Variant, hn As Variant, ntb As Variant, dnb As Variant, _
    tnbBS As Variant, tnbNS As Variant, hcm1 As Variant, hcm2 As Variant, _
    hcm3 As Variant, a As Integer

It is not the same as 它不一样

Dim btb As Integer, dbb As Integer, hn As Integer, ntb As Integer, dnb As Integer, _
    tnbBS As Integer, tnbNS As Integer, hcm1 As Integer, hcm2 As Integer, _
    hcm3 As Integer, a As Integer

A single project can contain up to 32,000 "identifiers" (any nonreserved keyword), which include, but are not limited to, forms, controls, modules, variables, constants, procedures, functions, and objects. 单个项目最多可包含32,000个“标识符”(任何非保留关键字),包括但不限于表单,控件,模块,变量,常量,过程,函数和对象。 Note that the actual number of identifiers is limited to available memory. 请注意,标识符的实际数量仅限于可用内存。

Variable names in Visual Basic can be no longer than 255 characters, and the names of forms, controls, modules, and classes cannot be longer than 40 characters. Visual Basic中的变量名称不能超过255个字符,表单,控件,模块和类的名称不能超过40个字符。 Visual Basic imposes no limit on the actual number of distinct objects in a project. Visual Basic对项目中不同对象的实际数量没有限制。

Code Limitations 代码限制

The amount of code that can be loaded into a form, class, or standard module is limited to 65,534 lines. 可以加载到表单,类或标准模块的代码量限制为65,534行。 A single line of code can consist of up to 1023 bytes. 单行代码最多可包含1023个字节。 Up to 256 blank spaces can precede the actual text on a single line, and no more than twenty-four line-continuation characters ( _) can be included in a single logical line. 在一行上实际文本之前最多可以有256个空格,并且单个逻辑行中不能包含不超过24个行继续符(_)。

Procedures, Types, and Variables There is no limit on the number of procedures per module. 过程,类型和变量每个模块的过程数量没有限制。 Each procedure can contain up to 64K of code. 每个过程最多可包含64K的代码。 If a procedure or module exceeds this limit, Visual Basic generates a compile-time error. 如果过程或模块超出此限制,Visual Basic将生成编译时错误。 If you encounter this error, you can avoid it by breaking extremely large procedures into several smaller procedures, or by moving module-level declarations into another module. 如果遇到此错误,可以通过将极大的过程分解为几个较小的过程或将模块级声明移动到另一个模块来避免此错误。

Visual Basic uses tables to store the names of identifiers (variables, procedures, constants, and so on) in your code. Visual Basic使用表来在代码中存储标识符的名称(变量,过程,常量等)。 Each table is limited to 64K. 每张桌子限制在64K。

DLL Declare Table Each form and code module uses a table that contains a structure describing a DLL entry point. DLL声明表每个表单和代码模块使用一个表,该表包含描述DLL入口点的结构。 Each structure uses approximately 40 bytes, with a total restricted size of 64K, resulting in roughly 1,500 declarations allowed per module. 每个结构使用大约40个字节,总限制大小为64K,每个模块允许大约1,500个声明。

Project-Name Table The entire application uses a single table that contains all names. 项目名称表整个应用程序使用包含所有名称的单个表。 These include: 这些包括:

Constant names 常名

Variable names 变量名称

User-defined — type definition names 用户定义的 - 类型定义名称

Module names 模块名称

DLL-procedure declaration names The project name table is unlimited in total size, but is limited to a total of 32K case-sensitive unique entries. DLL过程声明名称项目名称表的总大小不受限制,但总共限制为32K区分大小写的唯一条目。 If the limit is reached, reuse private identifiers in different modules to limit the number of unique entries to 32K. 如果达到限制,则在不同模块中重用私有标识符以将唯一条目的数量限制为32K。

Import Table Every reference to an identifier in a different module creates an entry in the Import Table. 导入表对不同模块中的标识符的每次引用都会在“导入表”中创建一个条目。 Each such entry is a minimum of 24 bytes and is restricted to 64K, resulting in roughly 2,000 references per module. 每个这样的条目至少为24个字节,并且限制为64K,每个模块大约有2,000个引用。

Module-Entries Table This table accepts up to 125 bytes per module, with a total limit of 64K, resulting in about 400 modules per project. 模块条目表该表每个模块最多可接受125个字节,总限制为64K,每个项目大约有400个模块。

The following limitations apply to variables in the Visual Basic language. 以下限制适用于Visual Basic语言中的变量。

Form, Standard, and Class Module Data The data segment (that is, the data defined in the Declarations section) of the VBA module of any form or module in Visual Basic can be up to 64K. 表单,标准和类模块数据Visual Basic中任何表单或模块的VBA模块的数据段(即“声明”部分中定义的数据)最大可达64K。 This data segment contains the following data: 此数据段包含以下数据:

Local variables declared with Static. 用Static声明的局部变量。

Module-level variables other than arrays and variable-length strings. 除数组和可变长度字符串之外的模块级变量。

4 bytes for each module-level array and variable-length string. 每个模块级数组和可变长度字符串的4个字节。 Procedures, Types, and Variables If a procedure or module exceeds the 64K code limit, Visual Basic generates a compile-time error. 过程,类型和变量如果过程或模块超出64K代码限制,Visual Basic将生成编译时错误。

If you define a procedure that has more than 64K of local variables defined, you get the error "Too many local nonstatic variables." 如果定义了一个定义了超过64K局部变量的过程,则会出现“太多本地非静态变量”错误。

If you define a module that has more than 64K of module-level variables defined, or if you define a User-Defined Type larger than 64K, you get the error "Fixed or static data can't be larger than 64K." 如果定义的模块定义了超过64K的模块级变量,或者定义了大于64K的用户定义类型,则会出现错误“固定或静态数据不能大于64K”。

If you encounter this error, you can avoid it by breaking extremely large procedures into several smaller procedures, or by moving module-level declarations into another module. 如果遇到此错误,可以通过将极大的过程分解为几个较小的过程或将模块级声明移动到另一个模块来避免此错误。

An array declared as a variable doesn't contribute to the entire size of the array; 声明为变量的数组不会影响数组的整个大小; only the array descriptor counts toward the 64K limit. 只有数组描述符计入64K限制。 So it is acceptable, for example, to have a declaration such as Dim x(1000000) As Byte either in a procedure or at module level. 因此,例如,在过程或模块级别中具有诸如Dim x(1000000)As Byte的声明是可接受的。 Out of memory problems occur, however, if you declare a large, fixed-size array in a record, then declare instances of those records as variables. 但是,如果在记录中声明一个大的固定大小的数组,则会出现内存不足问题,然后将这些记录的实例声明为变量。

User-Defined Types No variable of a user-defined type can exceed 64K, although the sum of variable-length strings in a user-defined type may exceed 64K (variable-length strings occupy only 4 bytes each in the user-defined type; the actual contents of a string are stored separately). 用户定义的类型用户定义类型的变量不能超过64K,尽管用户定义类型中的可变长度字符串的总和可能超过64K(可变长度字符串在用户定义的类型中每个只占用4个字节;字符串的实际内容是分开存储的)。 User-defined types can be defined in terms of other user-defined types, but the total size of the types cannot exceed 64K. 用户定义的类型可以根据其他用户定义的类型定义,但类型的总大小不能超过64K。

Stack Space Arguments and local variables in procedures take up stack space at run time. 堆栈空间过程中的参数和局部变量在运行时占用堆栈空间。 Module-level and static variables do not take up stack space because they are allocated in the data segment for forms or modules. 模块级和静态变量不占用堆栈空间,因为它们是在表单或模块的数据段中分配的。 Any DLL procedures you call use this stack while they are executing. 您调用的任何DLL过程在执行时都使用此堆栈。

Visual Basic itself uses some of the stack for its own purposes, such as storing intermediate values when evaluating expressions. Visual Basic本身使用某些堆栈用于其自身目的,例如在计算表达式时存储中间值。

Total available stack size for Visual Basic is one megabyte (1MB) per thread. Visual Basic的总可用堆栈大小是每个线程1兆字节(1MB)。 A stack may grow beyond this, however, if there is adjacent free memory. 但是,如果存在相邻的空闲内存,则堆栈可能会超出此范围。

For More Information For tips on conserving stack space, see "Designing for Performance and Compatibility." 有关详细信息有关节省堆栈空间的提示,请参阅“设计性能和兼容性”。

From https://msdn.microsoft.com/en-us/library/aa716295(v=vs.60).aspx ‎ and nearby pages. 来自https://msdn.microsoft.com/en-us/library/aa716295(v=vs.60).aspx和附近的网页。

There are at least two things wrong with your code: 您的代码至少有两个问题:

  1. Declaration is incorrect 声明不正确

     Dim btb, dbb, hn, ntb, dnb, tnbBS, tnbNS, hcm1, hcm2, hcm3, a As Integer 

    Multiple declarations on a single line are allowed, but each variable needs its own As part. 允许在一行上进行多个声明,但每个变量都需要自己的As部分。 Currently, only a is declared as Integer , the others will be Variant . 目前,只有a被声明为Integer ,其他的将是Variant Change to: 改成:

     Dim btb As Integer, dbb As Integer, hn As Integer, ntb As Integer, dnb As Integer, tnbBS As Integer, tnbNS As Integer, hcm1 As Integer, hcm2 As Integer, hcm3 As Integer, a As Integer 
  2. Multiple assignments on a single line are not allowed, only the first one is performed. 不允许在一行上进行多次分配,仅执行第一次分配。 So in this case: 所以在这种情况下:

     btb = dbb = hn = dnb = tnbBS = tnbNS = hcm1 = hcm2 = hcm3 = a = 0 

    only the value of btb is changed to False as 只有btb的值改为False as

     dbb = hn = dnb = tnbBS = tnbNS = hcm1 = hcm2 = hcm3 = a = 0 

    evaluates to False . 评估为False

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

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