Declaring Constants

Coryj5

Board Regular
Joined
Sep 27, 2011
Messages
76
I am currently studying VBA and im currently on a chapter about declaring variable and constants.

I was hoping someone could help explain a little more clearly why one would bother declaring a constant.
The example in the book is:
Const myMonths as Integer = 12

Why would you not use 12 as the code throughout the macro if it was never going to change, being a constant.

Also, why not use the same Dim myMonths as Integer?

Thanks,
Coryj5
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Mainly for tidiness. When you're reading the code you can see what you're referring to. Otherwise you might forget why you were (example) dividing by 12, if you divide by numMonths then you (and anyone else looking at your code) will always understand why.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
It might be stuff you'll change occasionally as well. I usually CONST version numbers of stuff I put together.<o:p></o:p>
<o:p></o:p>
There are probably a host of other excellent reasons as well but those are the two that leap to mind.<o:p></o:p>
<o:p></o:p>
Regards<o:p></o:p>
Adam<o:p></o:p>
 
Upvote 0
Constant variables can often make your macros easier to maintain, as you need only change a value on one line instead of many. They help in documenting your macros as a variable name has meaning while a number often doesn't. For example, interestRate has far more meaning than 0.12.
 
Upvote 0
Ok, i was thinking it was something along those lines.

Thanks for the quick reponses!

Coryj5
 
Upvote 0
If you create a variety of procedures on different module sheets and decide to use Constants you might find it easier to create a separate module to maintain all the Constants in one place, you would then have to declare each Constant as Public.

I find that having a separate module sheet for Constants easier to manage. I would also name the module sheet like modConst.

Example could be using file paths, message & input box titles.

Public Const strTitle = "Learning VBA"
Public Const strPath = "C:\My Documents\"
Public Const strAltPath = "D:\Data Lists\"

Sample using the const strTitle

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> msg1()<br>MsgBox "Hello I am learning VBA", vbInformation, strTitle<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

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