0

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

Dim yearnr As Integer = 18

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2703472
  • 45
  • 2
  • 6
  • 12
  • 1
    Possible Duplicate [Of This Question](http://stackoverflow.com/questions/3256122/can-i-simultaneously-declare-and-assign-a-variable-in-vba) – user2140261 Oct 16 '13 at 14:42
  • Could you ... add.. the error message? – Jens Bergvall Oct 16 '13 at 14:50
  • When you say the answers offered below "didn't work" please indicate what happens. Is there an error message? Etc. All of the answers provided are syntactically correct. If they are not working, I suspect you are not implementing them correctly. – David Zemens Oct 16 '13 at 15:43

3 Answers3

2

In VBA the above format is not possible

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 
David Zemens
  • 53,033
  • 11
  • 81
  • 130
Running Forward
  • 90
  • 1
  • 1
  • 11
1

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.

Dim yearnr As Integer
yearnr = 18
dezzie
  • 21
  • 2
1

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

user2140261
  • 7,855
  • 7
  • 32
  • 45