{"id":12135,"date":"2012-04-23T07:13:59","date_gmt":"2012-04-23T07:13:59","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1584"},"modified":"2022-08-12T11:12:38","modified_gmt":"2022-08-12T11:12:38","slug":"excel-programming-vba-variable","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/04\/excel-programming-vba-variable\/","title":{"rendered":"VBA Programming : Variables in VBA"},"content":{"rendered":"
Just like other programming Languages, Excel VBA also uses different types of Variables. This Article teaches you the following Topics:<\/p>\n
Variable<\/strong> is a Symbolic Names given to a Memory Location which contains a specific or non-specific Information, Data or Value. \nYou can name anything to your Variable but it is always advised to provide a valid name to your variable. By “Valid Name” I mean, Variable Name should be based on What kind of data you want to Store in that variable. This is helpful when you have hundreds and thousands lines of VBA Codes.\n<\/p>\n You want to Declare a Variable which holds a Boolean Flag where the WorkBook is open or not. You can give the variable name as wbIsOpen<\/strong>. Here wb = WorkBook. So by reading the variable name itself, it it will remind me that it will store Boolean value either Yes or No whether WorkBook is Open or Not.<\/p>\n This was just an example, you can give any understandable name, which help you in remembering this variable and referring it anywhere in the program.<\/p>\n By using a Keyword Dim<\/strong>, you can declare a variable. Dim is Short form of Dimension<\/strong>. Refer the below Syntax, how to declare a variable:<\/p>\n For example, i have a variable name as wbIsOpen<\/strong> of Boolean<\/strong> type. <\/p>\n \nIn Excel VBA, as soon as you type “As” after variable name, list of all possible variable types available in Excel will be listed in a drop down as shown below. This is called Intellisense<\/strong>\n<\/p>\n
\nBefore we get into much details about Variables, I will tell you some Basic Rules about Variables<\/strong>. There are few important thing, you should know about before using Variables:<\/p>\n
\n2) A Variable name can have maximum of 250 characters.
\n3) A Variable Name can not be same as an Existing KeyWords available in Excel VBA. For Example: For, Next, while, loop etc.
\n4) Variable Names can not have SPACE<\/strong> between two words, if you want to Name a variable with two or more words. You Can connect two or more words by UnderScore ( _ ) but can not have SPACE.\n<\/div>\nFor Example:<\/h2>\n
Note:<\/h3>\n
How to Declare a Variable ?<\/h1>\n
\r\nDim wbIsOpen As Boolean\r\n<\/code>\r\n<\/pre>\n