Understanding the VBA Syntax

Excel VBA for Beginners 1-Introducing Excel VBA (Visual Basic for Applications)
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.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 going to speak about the VBA speech or the VBA syntax or how the VBA language is written. So VBA is an object oriented language as we've explained before, and that means that objects are nouns they come first. So in order to understand the VBA syntax, or how the VBA language is written, we're going to actually compare it to how the English language is written. So, in English, you would say, take the bowl, however, in VBA, objects or nouns are most important, so they come first. So if you want to say kick the bowl, using VBA, you would put the noun first.

So if you want to write a sentence using VBA, you would write the noun first. So you'd write a noun or a verb or object method and therefore If you want to say kick the bowl using the VBA syntax, you would say bro, dot cake. So as you can see here, the noun which is the bowl comes before the verb, which is kicked. So it's a bowl cake if you want to say in VBA syntax, some other examples as well, that you would write using VBA syntax is like dinner dot eat, or car dot drive. So if you want to open a workbook in VBA, you would say workbook dot open. If you want to activate a worksheet, you would say worksheet dot activate.

Some examples of objects or nouns in Excel, of course are the cell is an object of workbook, the worksheet, a chart as well as an object, and we've explained that before. Some examples as well of collection we've explained collections before in Excel VBA. So some examples of collections, or they are considered as actually plural nouns. Are the cells collection, which is a plural or it contains cells, and also the worksheets collection, which contains worksheets and workbooks collection, which contains all workbooks as well. So we're just reiterating the idea of having collections in Excel VBA. Now, we're speaking about collections because we want to speak about how to identify items in a collection.

So if you've got a collection of items, how do you identify a certain item in a collection to make sure that you use the right item or the item that you intend to use? So for example, if you've got three bowls here, we've got a soccer or a football bowl, and we've got a bowling bowl and we've got a basketball. So how do we make sure that we pick the right bowl which is if we're going to kick a bowl, it should be the soccer bowl. Right, we don't want to kick the bowling ball and we don't want to kick the basketball. So there are actually two ways of identifying an item in a collection. There is a less used way and a less recommended way, which is using the index number.

And although it's a less used way, you will use it sometimes, but you're not going to use a lot as the other way. And the other way is using the name, but let's speak about using the index number first. So if you want to use the index number, and you want to kick the first bowl in this collection, and the first is basically counting from left to right, then you would write bowls, one dot kick, so if you write bowls, one dot kick, this way, you will be able to kick the soccer ball. However, what happens if these bowls got reordered? And that means what happens if one of your friends puts The bowling bowl to be the first bowl from the left hand side and you write bowls, one dot kick and you kick the bowling bowl. Ouch, that's going to be a painful experience, right?

It's going to be very painful to kick a bowling ball. So this is why it is not recommended to use the index number to identify an item in a collection. So the better way is to actually use the name of the object so it's better to write rolls software soccer dot kick. So if you do that, you will make sure to kick the correct bowl which is the soccer bowl, regardless of its position in this collection. So if the soccer ball is the first or the second or the third, you're going to make sure that the soccer bowl is the bowl, you kick, okay, so it's better to use the Name of the item rather than its position. Now let's speak about methods or verbs.

So, methods in Excel VBA or verbs have actually parameters and parameters act like adverbs in the English language. So if you say bowls soccer dot kick, and then you would write direction colon equals right force column equals hard the direction and the force are considered parameters are adverbs in Excel VBA. And they describe how the verb or the method which is kick is done. So here we're saying that we want to kick the ball to the right and with a hard force, so if you see a call an equals operator, this indicates that you've got an adverb or a parameter, and parameters are adverbs can sometimes be be confusing, because if you know the default order of the adverbs, you can leave off the name. So if you know that the default order for the direction and the fours are that the direction comes first and then the force parameter, then you could write instead of writing bull sucker kick direction colon equals write force colon equals hard.

You could write bull soccer kick, right, comma hard. Of course, we do not have a kick method in Excel VBA. And we do not have a direction and a force parameters, but you get the idea. So in VBA, you could write shapes dot add shape, so we're using the Add Shape Method on the shapes collection object in order to add a new shape. And then you would write left column equals 10, top column equals 20 width column equals 30, height column equals 15. These parameters are adverbs identify the position of the shape when it is inserted.

So you can actually write shapes dot add shape 10 comma 20 comma 100 comma 15 without explicitly writing the parameters and it will still work, okay, the code will work absolutely fine. However, if you do that, if you write the code without explicitly writing the parameters, the code will be confusing, right? If someone else reads your code, and you write shapes that add shape 10 comma 20 comma 100 comma 15. If somebody reads your code, this way, they will be confused, they will not understand what you mean, because not everyone remembers the positions of the parameters or the order of the parameters, right. So writing the values of the parameters without specifying the actual parameters is a bad programming practice. So Even though your code is still going to work, it is actually a bad programming practice.

And it is better to explicitly write your parameters or adverbs. Okay, so we've spoken about parameters. Now let's speak about properties or adjectives. So properties, what they do is that they describe an object. So for example, when you write active sheet dot name equals my sheet, the property name here gets the value of my sheet, and that means that the name of the active sheet is going to be my sheet and also properties do not use the colon equals construct. Okay, so the colon equals is exclusive for parameters.

So whenever you see a colon equals, that means you've got a parameter here, and you can set a variable equal to the value of a property. So for example, you can set variable x to be equal to the name of the active worksheet by writing x equals active sheet name and we will see how to do that in the future on the course as well. Okay, so let's summarize what we've explained on this video. So a noun in the English language is equivalent to an object in Excel VBA. And a plural noun is equivalent to a collection. And think of different collections as different families.

A verb in the English language is equivalent to a method in Excel VBA. And an adverb is equivalent to a parameter and also an adjective in the English language is equivalent to a property in Excel VBA. Okay, so I hope that was informative for you about understanding the VBA syntax. 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.