详细说出声明的来龙去脉,可以不举例

解决方案 »

  1.   

    很少有关这方面的内容,一般书上说:声明可使程序可读性强。不声明时,隐藏的变量默认都是Variant,占用资源最大。
    所以不知道声明有什么来龙去脉,下面是MSDN的说法:
    Visual Basic for Applications ReferenceDim Statement
          Declares variables and allocates storage space.SyntaxDim [WithEvents] varname[([subscripts])] [As [New] type] [, [WithEvents] varname[([subscripts])] [As [New] type]] . . .The Dim statement syntax has these parts:Part Description 
    WithEvents Optional. Keyword that specifies that varname is an object variable used to respond to events triggered by an ActiveX object. WithEvents is valid only in class modules. You can declare as many individual variables as you like using WithEvents, but you can't create arrays with WithEvents. You can't use New with WithEvents. 
    varname Required. Name of the variable; follows standard variable naming conventions. 
    subscripts Optional. Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax: 
    [lower To] upper [, [lower To] upper] . . .When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.
     
    New Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsic data type, can't be used to declare instances of dependent objects, and can’t be used with WithEvents. 
    type Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable you declare. 
    ResVariables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.Use the Dim statement at module or procedure level to declare the data type of a variable. For example, the following statement declares a variable as an Integer.Dim NumberOfEmployees As IntegerAlso use a Dim statement to declare the object type of a variable. The following declares a variable for a new instance of a worksheet.Dim X As New WorksheetIf the New keyword is not used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object.You can also use the Dim statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.If you don't specify a data type or object type, and there is no Deftype statement in the module, the variable is Variant by default.When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it were a separate variable.Note   When you use the Dim statement in a procedure, you generally put the Dim statement at the beginning of the procedure.Visual Basic for Applications ReferencePublic Statement
          Used at module level to declare public variables and allocate storage space.SyntaxPublic [WithEvents] varname[([subscripts])] [As [New] type] [,[WithEvents] varname[([subscripts])] [As [New] type]] . . .The Public statement syntax has these parts:Part Description 
    WithEvents Optional. Keyword specifying that varname is an object variable used to respond to events triggered by an ActiveX object. WithEvents is valid only in class modules. You can declare as many individual variables as you like using WithEvents, but you can't create arrays with WithEvents. You can't use New with WithEvents. 
    varname Required. Name of the variable; follows standard variable naming conventions. 
    subscripts Optional. Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax: 
    [lower To] upper [,[lower To] upper] . . .When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.
     
    New Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsic data type, can't be used to declare instances of dependent objects, and can't be used with WithEvents. 
    type Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String, (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable being defined. 
    ResVariables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside.Caution   The Public statement can't be used in a class module to declare a fixed-length string variable.Use the Public statement to declare the data type of a variable. For example, the following statement declares a variable as an Integer:Public NumberOfEmployees As IntegerAlso use a Public statement to declare the object type of a variable. The following statement declares a variable for a new instance of a worksheet.Public X As New WorksheetIf the New keyword is not used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object.You can also use the Public statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.If you don't specify a data type or object type and there is no Deftype statement in the module, the variable is Variant by default.When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it were a separate variable.
      

  2.   

    变量是用来存储值的所在处;它们有名字和数据类型。变量的数据类型决定了如何将代表这些值的位存储到计算机的内存中。在声明变量时也可指定它的数据类型。所有变量都具有数据类型,以决定能够存储哪种数据。
    根据缺省规定,如果在声明中没有说明数据类型,则令变量的数据类型为 Variant。Variant 数据类型很象一条变色龙— 它可在不同场合代表不同数据类型。当指定变量为 Variant 变量时,不必在数据类型之间进行转换,Visual Basic 会自动完成各种必要的转换。
    但是,如果知道变量确实总是存储特定类型的数据,并且还声明了这种特定类型的变量,则 Visual Basic 会以更高的效率处理这个数据。例如,存储人名的变量最好表示成 String 数据类型,因为名字总是由字符组成。 
    除变量外,数据类型也用于其它场合。在给属性赋值时,这个值就有数据类型;函数的参数也有数据类型。事实上,在 Visual Basic 中,凡是与数据有关的东西就与数据类型有关。
    也可声明任何基本类型的数组。