Transcript for:
Understanding VBA Variable Declarations

okay so in this lecture we'll be talking about variable declaration and object initialization so in the below sub procedure which is declare you can see i have declared a lot of variables of different types don't worry i'll be going through all these one by one in the above sub procedure so in test you can see i'm using dim a as integer and i'm assigning a value a equals 10 i am simply displaying in a message box and the value get displayed over there so this is how we declare integers and if you want to perform any calculation let's say i add 10 to it or i subtract 3 from it i can perform everything on this integer value and it would be giving me a result as integer value so the type of this variable a was integer that's why i was able to perform integral calculations now i've removed the type declaration you see that it is still calculating the same thing that means the default data type which is variant data type in excel is automatically able to identify that it is integer and it should perform integral calculation on it now i convert this a to a string value i assign a string value to it i make it as a equals comma and let's say i add 10 to it it gives me a type error this type mismatch is occurring because i am assigning a string value to variable a and on the other hand i am trying to add a integer to this variable so that is not possible that's why it is saying that because you are trying to add a integer value in a string variable that is not possible and there is actually a type mismatch if you want to learn more about variables i recommend you to go to my youtube page which is extreme automation by kamal gerder and you will find a complete course on excel vba which is freely available for everyone you can find a lecture on variables and you'll be finding more detailed information in those tutorials now i remove this numerical part and i simply display what is stored in a and i can get this string printed on message box now i try to let's say concatenate a string to the string using ampersand operator now it should concatenate and give me the result now we have not declared the type of variable a we are directly assigning a string value and we are performing concatenation operation it is good that you always declare the type for string variables as string in this way so i copy it from here and i can paste it here so then be a string and you can then assign a value which is string and perform any operation related to strings all right let us move on to the next data type which is date so to declare a variable of type date we need to write demc as date and then we need to give the value while assignment we need to give it in double quotes this more or less looks like a string but you will see the difference i display the value let's say message box c so it will give me the value now you must be wondering that it is a string but if i add one to it we cannot add one to a string because that gives us a type mismatch error but a date when added with 1 it gives us the next day so that is a way of assigning values to a date type variable and then we can use it in date format we can add and subtract days to get other day now let us try one more thing let us remove the declaration of this variable c i just commented let us try to run it now it gives us type mismatch error because by default if you do not declare the type it takes it as string if the value comes in double quotes and it cannot add one to the string so that's the issue and that's why we need to declare the type as well moving on to the next data type which is boolean a boolean is a data type that takes only two values true and false so we declare it like dim d as boolean and then let's say we assign a value d equals true remember that true always have t capital and rest characters are small similarly for false f is capital rest all are small so let us add a message box to display the value of d so message box and then d if i run this piece of code the output is true on a list box i can use and or and not function so if i close this d in not function i run it again it gives us just reverse of it so for true it returns false and for false it returns true so now let us understand the tricky part of it let's say i am commenting this declaration and while assigning the value i'm not giving a boolean value rather i'm giving this boolean value in string format so d is actually a string but when i'm running i'm getting the boolean value false so basically why i'm getting this false as a result is because this not function first of all tries to convert the value that you are going to provide into the boolean value and only then it performs operation so even i if i am giving let's say all capitals that is not an actual or valid boolean value that is a string value so not function first tries to convert that capital true into the boolean value rule and then gives me the reverse of it so that's why we are getting false here so moving on to the next data type which is currency it is more or less similar to a numeric or you can say float or double data type so dem f as currency i give a let's say float value 2.3333 some numbers like this so one point to notice currency always take values up to four decimal digits and if you do not give or if you give more than those number of digits it automatically try to adjust it in that way so i've assigned this value and when i'm running this you can see that i'm getting value up to four digits only now let's say you want to deal with a number or a value which is having more digits after decimal places so ideally that data type should be doubled right so if you print this or if you run this code now you will get values up to that precision there is a shortcut for currency variables that while assigning value in the editor which is of currency type what you need to do is you just need to put a add the rate symbol after the value and it would automatically change your value in the editor itself and it won't evaluate that value in the later stage now let us try to understand the difference between integer and long so i declare a variable a of integer type and i assign a value from 1 to 9 and i display this value in message box it gives me overflow error the reason is it is outside the range of integer data type i change the data type to long it now accepts the value and let's say i add 10 to it it works absolutely fine for me so if your value is inside the range of integer you should use integer data type if that value is outside it you should use long data type to see the exact ranges i would suggest you to go to microsoft official website and in the documentation you will be able to find the exact ranges for all the data types all right so next talk about very important and the default data type that we have in vba which is variant and to explain this i declare a variable of type integer and as i assign an integral value to it and later i assign a string value to it all right so you know that i would be getting type mismatch error if i do it like this way and i add message boxes to print the integer value as well as string value i run the code i get the integer value printed but for string value it gives me type mismatch error the reason is it is integer value and it cannot hold a string value if i change the data type from integer to variant it works absolutely fine so variant is the default data type where it tries to accommodate that value according to the value that you provide so next we'll be talking about objects so objects represent an element of an application that element can be a worksheet can be a word document can be an outlook application instance and can be any other application for example internet explorer that we would be exploring too much in our course where we would be launching internet explorer we would be opening some urls and then we'll be trying to perform actions on the pages so for such things where you want to access some applications you need to actually create an object or an instance of that particular application to declare an object what you need to do is dim j as object and then you need to use set keyword to assign value to this object you remember that we assigned value to variable directly like a equals 10 or a equals couple but to assign values to objects we need to use set keyword and then to create a new instance for any application you need to use new keyword so for example if i want to open a new excel application i will be using excel dot application and i would make this object visible we can set the property visible to true like this and if we run this piece of code you would see that excel application launched by this piece of code which is visible here okay so similarly we can deal with other applications as well but another important point to note is if you want to work with some other applications you need to import their references or libraries before using them for example if i use outlook.application and let's make it visible as well before i use outlook i need to go to tools references and from these references i need to find microsoft outlook object library i need to include it only then i would be able to launch outlook instance through excel vba similarly if you want to control internet explorer so i would be declaring it like this dem i e as object you can give any name to your object set ie equals new internet explorer now before you use it you need to go to references and you can remove this outlook because now you are not using it but for internet explorer you need to include microsoft html object library and microsoft internet controls okay so that's all for objects and variables