Variable Data Types

9 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.81
List Price:  €129.94
You save:  €37.13
£79.68
List Price:  £111.56
You save:  £31.87
CA$136.70
List Price:  CA$191.38
You save:  CA$54.68
A$151.47
List Price:  A$212.07
You save:  A$60.59
S$134.88
List Price:  S$188.84
You save:  S$53.96
HK$781.18
List Price:  HK$1,093.68
You save:  HK$312.50
CHF 90.49
List Price:  CHF 126.69
You save:  CHF 36.20
NOK kr1,087.35
List Price:  NOK kr1,522.33
You save:  NOK kr434.98
DKK kr692.92
List Price:  DKK kr970.11
You save:  DKK kr277.19
NZ$166.33
List Price:  NZ$232.87
You save:  NZ$66.53
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,943.35
List Price:  ৳15,321.13
You save:  ৳4,377.77
₹8,337.18
List Price:  ₹11,672.39
You save:  ₹3,335.20
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,738.77
List Price:  ₨38,835.40
You save:  ₨11,096.62
฿3,675.50
List Price:  ฿5,145.85
You save:  ฿1,470.34
₺3,235.04
List Price:  ₺4,529.19
You save:  ₺1,294.14
B$509.60
List Price:  B$713.47
You save:  B$203.86
R1,850.72
List Price:  R2,591.08
You save:  R740.36
Лв181.51
List Price:  Лв254.13
You save:  Лв72.61
₩135,590.93
List Price:  ₩189,832.73
You save:  ₩54,241.80
₪370.74
List Price:  ₪519.05
You save:  ₪148.31
₱5,705.78
List Price:  ₱7,988.32
You save:  ₱2,282.54
¥15,303.96
List Price:  ¥21,426.16
You save:  ¥6,122.20
MX$1,697.07
List Price:  MX$2,375.96
You save:  MX$678.89
QR363.69
List Price:  QR509.19
You save:  QR145.49
P1,358.38
List Price:  P1,901.79
You save:  P543.40
KSh13,211.65
List Price:  KSh18,496.84
You save:  KSh5,285.18
E£4,788.95
List Price:  E£6,704.73
You save:  E£1,915.77
ብር5,723.72
List Price:  ብር8,013.44
You save:  ብር2,289.71
Kz83,591.64
List Price:  Kz117,031.64
You save:  Kz33,440
CLP$94,219
List Price:  CLP$131,910.38
You save:  CLP$37,691.37
CN¥723.64
List Price:  CN¥1,013.13
You save:  CN¥289.48
RD$5,794.19
List Price:  RD$8,112.10
You save:  RD$2,317.90
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.06
List Price:  Q1,085.12
You save:  Q310.05
GY$20,860.22
List Price:  GY$29,205.14
You save:  GY$8,344.92
ISK kr13,949.49
List Price:  ISK kr19,529.85
You save:  ISK kr5,580.35
DH1,005.73
List Price:  DH1,408.07
You save:  DH402.33
L1,766.28
List Price:  L2,472.87
You save:  L706.58
ден5,712.52
List Price:  ден7,997.75
You save:  ден2,285.23
MOP$802.57
List Price:  MOP$1,123.63
You save:  MOP$321.06
N$1,845.78
List Price:  N$2,584.16
You save:  N$738.38
C$3,670.67
List Price:  C$5,139.09
You save:  C$1,468.41
रु13,313.56
List Price:  रु18,639.52
You save:  रु5,325.95
S/372.22
List Price:  S/521.13
You save:  S/148.90
K385.44
List Price:  K539.63
You save:  K154.19
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,689.66
List Price:  ZK3,765.63
You save:  ZK1,075.97
L462.03
List Price:  L646.86
You save:  L184.83
Kč2,326.92
List Price:  Kč3,257.79
You save:  Kč930.86
Ft36,152.68
List Price:  Ft50,615.20
You save:  Ft14,462.51
SEK kr1,078.64
List Price:  SEK kr1,510.15
You save:  SEK kr431.50
ARS$87,815.44
List Price:  ARS$122,945.14
You save:  ARS$35,129.69
Bs690.51
List Price:  Bs966.74
You save:  Bs276.23
COP$388,367.89
List Price:  COP$543,730.59
You save:  COP$155,362.69
₡50,962.55
List Price:  ₡71,349.61
You save:  ₡20,387.06
L2,463.20
List Price:  L3,448.58
You save:  L985.38
₲746,475.93
List Price:  ₲1,045,096.16
You save:  ₲298,620.23
$U3,821.56
List Price:  $U5,350.33
You save:  $U1,528.77
zł401.98
List Price:  zł562.79
You save:  zł160.80
Already have an account? Log In

Transcript

Hey guys welcome back so in this video we're going to discuss the different variable data types in Excel VBA. So, variables in Excel VBA have different data types. And if you think of variables as containers are buckets that contain data and this data is stored in memory, these containers are buckets number one can hold different types of data. So each bucket type or container type can hold a certain data type and it cannot hold other data types this number one, number two, is that because the hold different data types, they will have different sizes because different data types can occupy different sizes. So they have two main aspects they can hold different data types, and they will have different sizes. Now the reason we're learning about the data types in Excel VBA for variables is that because these variables occupy a size in the memory of your computer, it is a better practice to select the appropriate data type for each variable.

So that means that it is a better practice to select a data type that can just accommodate the data that you're going to put in your variable. And you would not select a data type that is overly big. So it is a better practice to just select a size that just fits your data as long as it can accommodate its size at all the points in your procedures. Okay, so as long as if the The size of your data shrinks or expands, it can accommodate it. This makes for better efficiency when your program or where your macro is running. So it makes sure that it runs smoother a runs in the shortest amount of time possible.

And although these differences do not appear in small procedures in small macros, they do appear in big macros, okay, so it is a better practice in general, to declare a variable that can just suit your data type that is not overly big, and it's also not smaller than the data type that you're gonna accommodate in your variable. Okay, so let's see what are the different data types that we have in Excel VBA. First of all, we've got the byte data type. And this byte data type has a storage size of one byte and that means that it's awkward occupies only one byte in memory of your computer. And it's all about ranges between zero and 255. So that means that you can put numbers between zero and 255 in that data type.

So if you put 256 for example, then you're gonna get an error. There is also the boolean data type and the boolean data type occupies two bytes in your memory in your computer's memory that's its storage size and it can only hold either true or false values okay. So it cannot take any other values it cannot take five or six and true or false By the way, they can be one and zero so one is a true and zero is a false. It cannot take any other values. It cannot take Johnny he cannot take five Okay, so can only Take true or false values, there is also the integer data type which can only hold integers and it occupies two bytes in your computer's memory. So, that is its storage size and it can accept values between negative 32,768 to positive 32,767.

So, if you try to put a value that is outside the range of values, so for example 33,000, you will get an error and if you try to put a decimal value in it, the value will be rounded up or down, depending on the value. So if the number on the right hand side of the decimal point is between one and five, it will be rounded down. So for example, if you give it a value between 1.0 and 1.5, it will be rounded down to a one However, if the value is greater than 1.5, so even 1.51 or 1.6, it will be rounded up to two. So that's that integer data type. There's also the long data type which occupies four bytes. And its range of values between negative 2,147,483,648 and positive 2 billion and all that long number.

There's also the single data type, which occupies four bytes of memory and has a range of negative 3.4 by 10 to the power 38 to negative 1.4 by 10 to the power negative 45. For negative values and 1.4 by 10 to the power negative 45 to 3.4 by 10 to the power 38. For positive values, has a big range, the double as well occupies eight bytes has this range negative one point 79 by 10 to the fourth to eight to negative four point 94 by 10, so forth three, negative three to four for negative values, and 4.9 by 10 to the point negative three to four to 1.79 by 10 to the power 308 for positive values, and there's the currency as well, you can see its range here. And there is also the date which can only hold dates, okay, so if you give it a string, it will fail on a string, it means a text basically, you will get an error.

There's also the string data type, and you can either declare a fixed length string, okay, and I'll tell you how to do that. And there's also the variable length string. And most in most of the cases, we just declare a variable length string because it's not worth it. To fix the length of the string. We normally just declare a variable length string, but it might show you how to do the fixed length one is very simple. Just for an extra bit of information, and there is also the variant data type.

And this data type is basically assumed by Excel, if you do not declare your variable as being a certain data type, so if you just declare a variable or if you do not declare a variable at all, Excel will just give it the variant data type, which as you can see here occupies the biggest amount of size in memory 16 bytes, and it is generally not a good practice to declare variant data types you might see me just not declare the variables in this course sometimes just to focus on the subject of the lesson that I will be teaching you. But it's generally not a good practice to leave your variables as variant it is a better practice to declare your various variables with a data type. And people would sometimes not declare the type of the variable if they do not know what is going to be put inside it and their macro would still run and probably this is the only exceptional case that you should do it.

But it's generally a better practice to specify the datatype for the variable that you declare, declaring variables is also debated. You will see debated on the internet because some people do not believe in the need for declaring the data types for variables are declaring the variables at all. And there are actually Excel geeks who actually do not declare variables such as for example Mr. Excel or bill jelen. He does not believe in declaring variables and declaring variables will not make a big difference when you write a small macro, but it does make difference when you write a big macro with lots of variables and lots of stuff to be done in it. So it is generally a better practice to declare your variables and declare their data types. And we will see in the upcoming lesson how we can do that.

But this is just an introduction to data types in Excel VBA. So thank you very much guys 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.