VBA - What is 'Dim' used for?

Burrgogi

Well-known Member
Joined
Nov 3, 2005
Messages
570
Office Version
  1. 2010
Platform
  1. Windows
I've just begun learning VBA - what does 'Dim' mean? I've seen that used quite often in VBA programming. (I've also seen Re-Dim).

And in the context of this particular thread, what does Dim cell as Range mean? (I know that a range can be considered to be a single cell or group of cells)
 
Dim is short for Dimension and is the way in which you define the data type for each variable.
 
Upvote 0
Dim is short for Dimension and is the way in which you define the data type for each variable.

So is it a way of telling VBA that you're working with text or numerical data?

In the context of the thread that I pointed out, what would happen if the programmer typed cell as range (leaving the 'Dim' out of macro).
 
Upvote 0
If you leave out the Dim keyword then the line will be invalid syntax so the code will not compile. In your VBA editor, type in the word Dim, select it with the mouse and then press F1 to bring up the 'Dim Statement' topic in the VBA helpfile. Lots of information in there.
 
Upvote 0
If you leave out the Dim keyword then the line will be invalid syntax so the code will not compile.

Not necessarily. The DIM statement is never necessary in VBA. It is only a standard by programmers as an attempt to avoid confusion when projects become very large.

E.G. - If your "Require variable declaration" option is OFF or there is no OPTION EXPLICIT statement in a module, VBA acts just like PHP:
Code:
message = "hello world!"

debug.print message
that'll output just the same as a DIMMED "message" variable.
 
Upvote 0
Not necessarily. The DIM statement is never necessary in VBA. It is only a standard by programmers as an attempt to avoid confusion when projects become very large.

E.G. - If your "Require variable declaration" option is OFF or there is no OPTION EXPLICIT statement in a module, VBA acts just like PHP:
Code:
message = "hello world!"
 
debug.print message
that'll output just the same as a DIMMED "message" variable.
I am interpreting Burrgogi's statement literally:
Burrgogi said:
In the context of the thread that I pointed out, what would happen if the programmer typed cell as range (leaving the 'Dim' out of macro).

ie.
Code:
cell As Range

which is syntatically incorrect.




The DIM statement is never necessary in VBA. It is only a standard by programmers as an attempt to avoid confusion when projects become very large.
It is extremely poor practice to implicitly declare variables and it should not be encouraged. Option Explicit has many more benefits than the one you suggest and should be used regardless of the size of the project. I listed some advantages here.
 
Last edited:
Upvote 0
As you might have guessed, implicit variable declaration is a pet peeve of mine! :biggrin:

Edit: hmmm.... maybe I'll set up a poll in the lounge to get an idea of the proportion of MrExcel VBA'ers who do and don't use Option Explicit?
 
Upvote 0
Re: VBA - What is 'Dim' used for? - C Legg's "Poll"

Colin,

I'm a great believer in Option Explicit. I'm an accountant who does a lot of VBA coding to try and bring finanaial controls to the Excel world. I can be notoriously careless in my own coding and design, so I think the dicipline enforced by declaring variables is an investment of time that repays itself many times over, both in debugging and, as importantly, documentation when you re-visit code after several months or projects. I never would have described this a "pet peeve" of my own but your useful comment struck a chord with me and I am 100% in agreement with you as to the importance of this issue.
Cheers.
 
Upvote 0
I would just like to add that many users (myself included) will not answer a thread with vba code with variables not declared.

Not declaring variables is a good way to waste time with errors that could be automatically detected by the compiler.

Notice that I do not mean that debugging a program is a waste of time, I've learned a lot debugging errors. That's not the case, however, when the errors were not caught because the variables were not declared. These type of errors are invariably dumb and debugging them is just a waste of time.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top