请教一个初级问题,关于vba 的dim写法

时间:2023-01-19 16:58:23
dim hstg3 as integer

dim hstg3, site as integer

中定义的hstg3 不同么?

我在实际程序里发现
用第一种定义方法
hstg3 = Nz(...) ‘ 这个hstg3 返回0

同一式,
用第二中写法,hstg3返回empty

测试在完全相同的条件下进行

8 个解决方案

#1


本帖最后由 wwwwb 于 2010-12-09 16:07:06 编辑
Dim hstg3, site As Integer
MsgBox VarType(hstg3) & "  " & VarType(site)

#2


本帖最后由 wwwwb 于 2010-12-09 16:06:39 编辑
Dim hstg1 As Integer, site As Integer
MsgBox VarType(hstg1) & "  " & VarType(site)

#3


那么没有集体定义多个变量的方法么?

#4


数组不行吗?

#5


啊,不是数组的问题,就是需要定义多个变量,
想一次定义多个变量

#6


引用 3 楼 ocean69 的回复:
那么没有集体定义多个变量的方法么?

没有

#7


VB 中就是这样,必须每个变量都说明。

#8


楼主如果肯稍微勤快一点,就没这么多问题了。

下面是ACCESS自带帮助中的内容。



Dim 语句示例
该示例演示使用 Dim 语句来声明变量,也演示了用 Dim 语句来声明数组。数组的缺省下界为 0,可以在模块级使用 Option Base 语句来取代数组的缺省下界。

'AnyValue 和 MyValue 按缺省情况被声明为 Variant,
'同时值被设为 Empty。
Dim AnyValue, MyValue

'显式声明一个 Integer 类型的变量。
Dim Number As Integer

'在一行中声明多个变量。AnotherVar 为 Variant 类型,
'因为它的类型被省略了。
Dim AnotherVar, Choice As Boolean, BirthDate As Date

'DayArray 是一个有 51 个索引(从 0 到 50)元素的 Variant 数组,
'假设在当前模块中 Option Base 被设为 0(缺省设置)。
Dim DayArray(50)

'Matrix 是一个二维 Integer 数组。
Dim Matrix(3, 4) As Integer

'MyMatrix 是一个显式指定了上下界
'的三维 double 数组。
Dim MyMatrix(1 To 5,  4 To 9,  3 To 5) As Double

'BirthDay 是一个索引从 1 到 10 的 date 数组。
Dim BirthDay(1 To 10) As Date        

'MyArray 是一个 variant 动态数组。
Dim MyArray()

#1


本帖最后由 wwwwb 于 2010-12-09 16:07:06 编辑
Dim hstg3, site As Integer
MsgBox VarType(hstg3) & "  " & VarType(site)

#2


本帖最后由 wwwwb 于 2010-12-09 16:06:39 编辑
Dim hstg1 As Integer, site As Integer
MsgBox VarType(hstg1) & "  " & VarType(site)

#3


那么没有集体定义多个变量的方法么?

#4


数组不行吗?

#5


啊,不是数组的问题,就是需要定义多个变量,
想一次定义多个变量

#6


引用 3 楼 ocean69 的回复:
那么没有集体定义多个变量的方法么?

没有

#7


VB 中就是这样,必须每个变量都说明。

#8


楼主如果肯稍微勤快一点,就没这么多问题了。

下面是ACCESS自带帮助中的内容。



Dim 语句示例
该示例演示使用 Dim 语句来声明变量,也演示了用 Dim 语句来声明数组。数组的缺省下界为 0,可以在模块级使用 Option Base 语句来取代数组的缺省下界。

'AnyValue 和 MyValue 按缺省情况被声明为 Variant,
'同时值被设为 Empty。
Dim AnyValue, MyValue

'显式声明一个 Integer 类型的变量。
Dim Number As Integer

'在一行中声明多个变量。AnotherVar 为 Variant 类型,
'因为它的类型被省略了。
Dim AnotherVar, Choice As Boolean, BirthDate As Date

'DayArray 是一个有 51 个索引(从 0 到 50)元素的 Variant 数组,
'假设在当前模块中 Option Base 被设为 0(缺省设置)。
Dim DayArray(50)

'Matrix 是一个二维 Integer 数组。
Dim Matrix(3, 4) As Integer

'MyMatrix 是一个显式指定了上下界
'的三维 double 数组。
Dim MyMatrix(1 To 5,  4 To 9,  3 To 5) As Double

'BirthDay 是一个索引从 1 到 10 的 date 数组。
Dim BirthDay(1 To 10) As Date        

'MyArray 是一个 variant 动态数组。
Dim MyArray()