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.

3
  • 1
    Possible Duplicate Of This Question Commented Oct 16, 2013 at 14:42
  • Could you ... add.. the error message? Commented Oct 16, 2013 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. Commented Oct 16, 2013 at 15:43

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

1 Comment

@user2703472 Added a ScrenShot of code in action when you say didn't work what happened??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.