这个变量的初始值有什么问题,Excel VBA

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

What is wrong with this variable with initial value , Excel VBA

excelvbavariables

提问by user2703472

I dot know why, Excel throws an error at this line:

我不知道为什么,Excel 在这一行抛出一个错误:

Dim yearnr As Integer = 18

Dim yearnr As Integer = 18

I want to declare a variable and assign a initial value. Please tell me why it is not working.

我想声明一个变量并分配一个初始值。请告诉我为什么它不起作用。

回答by Running Forward

In VBA the above format is not possible

在 VBA 中,上述格式是不可能的

only constants can be used like this :

只能像这样使用常量:

Const yearnr As Integer = 18

you can use

您可以使用

Dim yearnr As Integer
yearnr = 18 


Public yearnr As Integer
yearnr = 18 

回答by dezzie

Excel doesn't let you initialize values at declaration as they're already initialized to zero. To do what you want just break it up into two lines.

Excel 不允许您在声明时初始化值,因为它们已经初始化为零。要做你想做的事,把它分成两行。

Dim yearnr As Integer
yearnr = 18

回答by user2140261

Looking at the Question in my comment, For you needs it would be:

查看我评论中的问题,对于您的需要,它将是:

Dim yearnr As Integer:  yearnr = 18

you can make it one line with the :character

你可以把它和:字符放在一行

enter image description here

在此处输入图片说明