Declaring Variables

14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.84
List Price:  €129.98
You save:  €37.14
£79.48
List Price:  £111.28
You save:  £31.79
CA$136.74
List Price:  CA$191.45
You save:  CA$54.70
A$150.88
List Price:  A$211.24
You save:  A$60.36
S$135.02
List Price:  S$189.03
You save:  S$54.01
HK$781.40
List Price:  HK$1,093.99
You save:  HK$312.59
CHF 90.57
List Price:  CHF 126.80
You save:  CHF 36.23
NOK kr1,084.75
List Price:  NOK kr1,518.70
You save:  NOK kr433.94
DKK kr692.49
List Price:  DKK kr969.51
You save:  DKK kr277.02
NZ$166.11
List Price:  NZ$232.56
You save:  NZ$66.45
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,956.06
List Price:  ৳15,338.92
You save:  ৳4,382.86
₹8,347.75
List Price:  ₹11,687.19
You save:  ₹3,339.43
RM473.85
List Price:  RM663.41
You save:  RM189.56
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,772.29
List Price:  ₨38,882.31
You save:  ₨11,110.02
฿3,673.43
List Price:  ฿5,142.95
You save:  ฿1,469.52
₺3,227.91
List Price:  ₺4,519.21
You save:  ₺1,291.29
B$507.22
List Price:  B$710.14
You save:  B$202.91
R1,843.50
List Price:  R2,580.97
You save:  R737.47
Лв181.60
List Price:  Лв254.25
You save:  Лв72.65
₩135,529.28
List Price:  ₩189,746.42
You save:  ₩54,217.13
₪374.14
List Price:  ₪523.82
You save:  ₪149.67
₱5,714.50
List Price:  ₱8,000.53
You save:  ₱2,286.03
¥15,376.28
List Price:  ¥21,527.41
You save:  ¥6,151.12
MX$1,692.97
List Price:  MX$2,370.23
You save:  MX$677.25
QR364.12
List Price:  QR509.78
You save:  QR145.66
P1,360.02
List Price:  P1,904.08
You save:  P544.06
KSh13,423.65
List Price:  KSh18,793.65
You save:  KSh5,370
E£4,794.44
List Price:  E£6,712.40
You save:  E£1,917.96
ብር5,730.37
List Price:  ብር8,022.75
You save:  ብር2,292.37
Kz83,526.97
List Price:  Kz116,941.11
You save:  Kz33,414.13
CLP$94,107.58
List Price:  CLP$131,754.38
You save:  CLP$37,646.80
CN¥707.87
List Price:  CN¥991.05
You save:  CN¥283.18
RD$5,800.92
List Price:  RD$8,121.52
You save:  RD$2,320.60
DA13,449.71
List Price:  DA18,830.13
You save:  DA5,380.42
FJ$224.39
List Price:  FJ$314.16
You save:  FJ$89.76
Q775.96
List Price:  Q1,086.38
You save:  Q310.41
GY$20,884.44
List Price:  GY$29,239.05
You save:  GY$8,354.61
ISK kr13,954.60
List Price:  ISK kr19,537
You save:  ISK kr5,582.40
DH1,006.90
List Price:  DH1,409.70
You save:  DH402.80
L1,768.33
List Price:  L2,475.74
You save:  L707.40
ден5,716.08
List Price:  ден8,002.75
You save:  ден2,286.66
MOP$803.50
List Price:  MOP$1,124.94
You save:  MOP$321.43
N$1,847.92
List Price:  N$2,587.16
You save:  N$739.24
C$3,674.94
List Price:  C$5,145.06
You save:  C$1,470.12
रु13,329.02
List Price:  रु18,661.17
You save:  रु5,332.14
S/372.66
List Price:  S/521.73
You save:  S/149.07
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,692.78
List Price:  ZK3,770.01
You save:  ZK1,077.22
L461.79
List Price:  L646.53
You save:  L184.73
Kč2,323.07
List Price:  Kč3,252.39
You save:  Kč929.32
Ft36,167.88
List Price:  Ft50,636.48
You save:  Ft14,468.60
SEK kr1,082.52
List Price:  SEK kr1,515.58
You save:  SEK kr433.05
ARS$87,865.40
List Price:  ARS$123,015.07
You save:  ARS$35,149.67
Bs691.31
List Price:  Bs967.87
You save:  Bs276.55
COP$388,509.43
List Price:  COP$543,928.75
You save:  COP$155,419.31
₡51,021.71
List Price:  ₡71,432.44
You save:  ₡20,410.72
L2,466.06
List Price:  L3,452.59
You save:  L986.52
₲747,341.53
List Price:  ₲1,046,308.05
You save:  ₲298,966.51
$U3,819.97
List Price:  $U5,348.11
You save:  $U1,528.14
zł401.45
List Price:  zł562.05
You save:  zł160.59
Already have an account? Log In

Transcript

Hey guys, welcome back. So in this video, we're gonna learn how to declare variables. And we're going to have a demonstration of the different variable data types. So basically, in order to declare a variable, you just write the word dim. And then let's say I want to name my variable my var, for example, and then you write as, and then you're going to have here, a menu pop up, so you can start typing the type of the variable. This menu pops up as part of the intelligence or the autocomplete feature in Excel VBA.

So let's say I'm going to declare my variable as a byte, for example. So it's going to be the byte data type. And you could see here that as I type byte, it started to get selected from this IntelliSense menu. So then you can press tab on your keyboard, the top key on your keyboard and you can see here that Excel even Adjust the casing so as to make the word dim have the D as a uppercase as an uppercase letter, and the word byte as well. So now actually we have reserved an amount of space in the computer's ram or the computer's memory for a variable called Molly var. Okay, so we have told to the computer that we want to reserve an amount of memory for this variable.

And because this type is going to be a byte, then it's gonna take only one byte in memory. So now what we need to do is to actually populate the variable that is called my var with a value it is not ready to receive a value. So in order to give it a value, you can just write my var and then equals and then give it a value. So you need to actually give it a value between zero and 200. In 55, because this is the allowable range, so let's say I give it a value of zero. Now if I run my macro step by step, so I'm going to press f8 on my keyboard to run it step by step.

And you can see here that the value of Molly var is equal to zero after this step is executed. The value of money var is equal to zero, because this is the value of put in it. You can see here, this balloon appearing when I hover over it, and note as well, that if you give the mind var a value that is outside the allowable range, so let's say I'm going to give it 256 because it Stipe is a byte. And you can see here that the allowable range is between zero to 255, and the number must be an integer, then you're going to actually get an error. So if we run this macro, we're going to get an error is called the overflow error and it indicates that You have given the variable a value that is outside its allowable range. So you need to make sure that your variable receives a value that is within its allowable range at all points in your procedure.

Now I want to also create another variable, I'm going to call it Mali var two, and I'm going to make it as Boolean, for example. And you can see here as I start typing Boolean, it starts to appear on the IntelliSense feature or the autocomplete feature here, and I press tab here, and you can see here that Excel has completed the sentence. And one more advantage of declaring your variables is that usually Excel will use the intelligence feature to try and predict which variable you're trying to type. However, sometimes it does not work. So if I tried to type my var normally Excel should Give me some suggestions. But if it doesn't work, you can press Ctrl and space on your keyboard.

And you can see here that I'm getting some suggestions. And I'm getting actually the first two suggestions are correct. So it's suggesting that I want to type my var or my var to so actually on your computer, it could work without pressing CTRL and space. But if it doesn't work, just press control and space, and you're going to get some suggestions. So this is one more advantage of declaring variables. So because it's a Boolean, when I press equals, Excel will predict that I'm either going to put a value of true or false, there is no other value because Boolean only accepts true or false.

So it's going to give me suggestions here in case of the Boolean because it can easily predict the values is either true or false. There's no other option. So if I select true for example, and I press tab on my keyboard, now if I run this map CRO can see here my var is 255 replaced its value. So it's not to get an error and my var two is equal to true now when I run this line of code, and I'm running it step by step using the f8 key on my keyboard. Now of course, if you give the my var two variable a value other than true or false, you are going to get an error as well. Okay guys, so I've actually declared another variable called my date, and its type is going to be a date, because I want to show you how to write dates in Excel VBA.

Now, we could write my date is equal to Now in order to write dates in Excel VBA. There are several ways to do that. One way is to press the pound key on your keyboard which is by pressing Shift and three And then you write the date, but it needs to be in the American format. So if we want to write the fifth of June, for example is going to be Oh 605 2016, for example, and then we close with another pound sign. And I'm actually going to write that in cell E two. So I'm going to write this date in cell E two.

So if you run this macro, you can see here that the date has been written. This is the fifth of June, and actually appears here in the international or British format. Because my computer is formatted this way. My computer is formatted according to the international system, not the US system. So even though I've written it using the US system, it's appeared on my Excel as the international system because this is how my computer is formatted if your computer is formatted as you A system is going to appear as the US system. So that is one way of writing dates.

Another way is to use the date value function. And the date value function accepts the date as a string or as a text. So you need to put it between double quotes and you can just type the date as a text and it can accept actually the date in several ways. So for example, 12th of Jan 12, Jan 2016, this one is going to work and 12 jan comma 2016, this one is going to work as well gives you the same result. Okay, so it can recognize several ways of writing the date as a text. And even if I write it here, like the international format, so 1201 2016 is going to recognize as well and it's going to put it correctly in the cell.

However, the way for writing dates that I would recommend the most is actually the date serial function because this avoids any confusion on how to write dates. Because sometimes if the computer is formatted in the US system and you're writing the date in the international system, or vice versa, this could cause issues. However, the date serial function is actually the best way of writing dates in Excel VBA. And I would recommend you use that whenever you can try to stick to that one all the time, because it simply takes three inputs the year and the month and the day as integers. So you put the year for example, 2016, comma, the month is going to be January oh one, and then the day the 12th of January for example. Let's actually delete this one first.

Go Back to the Visual Basic Editor. If you run that, you will have your date typed correctly. So my recommendation, always stick to the date serial function for typing dates in Excel VBA. Now in this lesson, we're not going to try every data type, because some data types, for example, the longer than the single and the double are just going to work normally, as long as you give them values within their ranges. Okay, so we're trying to discuss the data types that could potentially cause you issues and that could be a bit tricky. Okay, so I've also declared another variable called my string here as a string.

And then we can actually type my string is equal to Joe, for example. So now the variable my string is going to be equal to Joe and we can display that in a message box message box. My string. So if you run this macro here, we're going to have a message box as well pop up with my string. And actually note that the message box function, and we'll speak about it in detail later on in the course, but it does accept the string data type. So you need to make sure that you're putting a string in the message box.

And it can actually, if you're displaying a number, it will automatically convert it to a string data type. So if the number is an integer data type, for example, in most of the cases, it will do an automatic conversion and converted automatically to a string data type. So the problems do not arise often, but you just need to know that it should accept a string, but we're going to discuss it later on in detail as well. Now the string variable that we have just declared here is actually a variable length. So that means it can accept pretty much any letter For the string, so zero to 2 billion characters this a lot, so pretty much any length, if you needed to be limited, you just type a star and then for example, I'm going to make it limited to two characters. If you run that you're not going to get an error, you just gonna have the E for Jo shaved off, okay, so there's going to be j and o only because it will only allow two characters.

But for all practical purposes, you're usually not going to see people declare the string variable as a limited string, it's going to be just a variable string, so you're not going to see the star and then the number. for practical purposes, you're very rarely going to see that people are not going to do that. Usually, when you see a string variable declared, it's just going to be a variable string, which is just dim, the name of the variable my string as string, and that's it. Now I've created another procedure here called variant data type to explain the variant data Type. And the variant data type is simply just a jack of all trades, it can accept values of any type. Okay, so the variant data type, you can declare a variable as a variant by writing dim my var for example as variant.

So this declares it as a variant. And also if you don't write it at all and just write my var is equal to jack, for example, okay, so this is a dice, and then messagebox my var, this also works. And if you run that macro, you can see here, jack appears in the message box. So because my var was not declared here at all, Excel VBA will just assume it's a variant data type, it will just give it a variant data type. So the variant data type happens when you either declare a variable as a variant and give it a value and you can take any value or not declare it at all, if you don't declare it at all. It will also We'll be given the variant data type.

And the variant data type can of course accept other types of values. So it can accept texts here, as we've seen, it can also accept dates, it can accept anything you can accept Boolean values can accept basically anything is just jack of all trades. And as you can see here, as well, we've written the variable without needing to declare it and the macro worked fine. But despite that, it is actually a best practice to declare the variables that you're going to use and declare them with the appropriate data types and not to declare them all as variant for example. So give them appropriate data types in order to make your macro as efficient as possible. This does not make a difference on small macros, but it does make a difference on big macros with lots of calculations and lots of stuff to be done.

Okay, so this was it, guys for declaring the variables. Thank you very much for watching this video and I'll see you on the next one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.