Placeholder Image

Subtitles section Play video

  • Developing applications in Excel VBA is often full of challenges and frustrations, but it doesn't have to be that way.

  • Hi, this is Randy with Excel for Freelancers, and in today's VBA basic training, we're going to go over VBA best practices so you can optimize and streamline your coding in order to create best applications you can in as little time with as little effort as possible and without those headaches.

  • It's going to be a great training, so let's get started.

  • Just a note before, I do create these VBA basic trainings each and every Saturday, along with Tuesday complete application development, so make sure you do get subscribed and also turn on the notification icon.

  • That'll ensure that you do get these trainings each and every week.

  • I create additional training on our Patreon platform.

  • Make sure you join us on Patreon, and I also have 300 of my best Excel templates that you can download with a link down below, so that'll also help you create applications.

  • All right, so today I want to go over VBA best practices.

  • We're going to go over what's the best way to write code, how to comment in, what are the correct names to use, how to develop programming modularly so you can easily view, edit, and update your code, and also what techniques on how to optimize your code.

  • So let's get into it right away.

  • First thing we want to do is make sure that we get inside the actual VBA editor, and to do that, we're going to go into the developer and the visual basic here, and then what we're going to do, if we don't have the developer, of course, you can just simply right-click here, and then we're going to customize the ribbon, and then just make sure developer selected.

  • You can also use a shortcut key, Alt F11.

  • Once you're in the VBA, you will see something like this, and this is the VBA editor, and inside this VBA editor, we have our sheets.

  • We only have one sheet right now, and we don't have any modules.

  • So when we're writing our code, most of the time we're going to be writing our code inside a module.

  • So the first thing we want to do is to make sure that we do add a module, and so we're going to right-click anywhere, and we're going to insert module.

  • You can also, from here, insert the module, and the first thing you want to do, as far as best practices are concerned, is naming that module.

  • When I first started developing, I didn't know how to name these modules.

  • I would have modules one, two, three, so if we insert another one here, and so now we have module two, and I didn't know what was inside each.

  • I would have to click on each just to know, so the first thing you want to do, especially if you're going to be developing a larger application with a lot of modules, is to name your module, and to do that, you can go into the properties here, or click F4, and it's going to launch this properties.

  • Now this window may be snapped here, it may be down here, it may be somewhere, depending upon your screen, but what you want to do is you want to give it a name.

  • Whatever the application you're developing is, let's say a customer application, we might want to do customer macros, right?

  • So we have different macros, maybe we have a scheduling macro, and then so the first thing you want to do is name your modules, and that's going to make things a lot easier, so let's just do something like scheduling macros.

  • Now keep in mind that when we do naming, we can't have any spaces, so if we try to add a module name with space, it won't allow us, so we want to make sure we're going to use underscores for spaces, so something like this, and you can abbreviate as long as it's clear to you when you go in exactly what the names are and what the consistencies.

  • Now while we're on naming, let's say we're in the customer macros, we're inside this module here, we want to also name our subroutines clear and easily, we want to use consistency throughout.

  • If you're developing a large application, those names are going to come in handy.

  • When we need to then assign those names to different buttons and associates, we want to be able to quickly and easily remember them.

  • Also when you're naming, we want to make sure that we use a combination of capital letters and smaller letters, so let's say I'll do customer, something like add new, and then maybe we'll do customer, and then we'll do save or update.

  • Now one of the things that I like, let's do the sub first, now when I want to add this new customer somewhere else, and if I'm in another module here, and I write sub schedule, and then add new, so maybe I want to add that new customer, and a good way if I want to remember it, I don't want to necessarily look over, using the combination of lowercase and uppercase is going to be really helpful, because it helps us let us know when we have the right one.

  • So if I type in add new, you see how it automatically goes to the capital letters, the proper name here.

  • However, if I get anything wrong inside the name, something like this, and I go over, look how it doesn't change, that's going to tell me right there, I've got something wrong with the name, so it kind of helps us, we don't always have to look back, so that tells me right there, because it didn't go to capital letters, I know that I might have an issue with the macro, so that kind of avoids errors, and makes things really clear.

  • So when we actually create our macro names, we want to use a combination of upper and lowercase, and that way when we write them out, and we're on another module, we don't quite remember, we type in all lowercase, and we make sure that it goes to the proper.

  • Now along the lines of naming, we've got naming the modules, we've got naming the macros, we also want to name our variables.

  • I see a lot when people program something like this, let's say we're going to do dimension

  • I as long, X as long, and then we'll do dimension C as string.

  • For me, that doesn't really help me, I don't remember, maybe it's my memory, but when I get back into this application, I'm not going to remember what I is, or X is.

  • And then we'll do something like for I equals 1 to 10, next I.

  • So here we see a loop, but I don't understand what I is, what is it?

  • Is it a row?

  • Is it a number?

  • Is it a customer?

  • I don't understand.

  • So we want to make sure that when we're naming our variables, it's very, very clear to us and everybody else.

  • Even if you're not teaching like I am, you want to make sure that when you come back to the application, we understand exactly the purpose of every variable.

  • And that's why naming variables is so critical.

  • I would do something like this, customer row, again, using the combination of capital and lowercase letters is going to really help us.

  • Customer row as long, and we'll do customer column as long.

  • And then we can do something like customer name as string.

  • So it's very clear the purpose of our variables simply by looking at the variables.

  • And I think that's a really critical component because when we're looking at our code or somebody else is looking at our code, or even if we're looking at our own code, a few years later, we want it to be very, very easy.

  • So once again, we can also use the variable.

  • If I say for customer row equals 1 to 10, next customer row, again, see, I'm typing in small letters.

  • If it then goes to the upper, I know that I've got the right variable.

  • So it's a great way.

  • So if I get it wrong, it's going to tell me right away that it hasn't changed the uppercase here.

  • So I know that there's something wrong with the variable.

  • So not only using correct names, but using a combination of upper and lowercase to help us quickly understand that means as we're typing the code, we know that we've got an incorrect variable and that happens all the time.

  • So we want to use not only naming the correct names and clear names, but using a combination of upper and lowercase or using some type of a proper name so that we can quickly see when we type incorrect variables.

  • And that happens to me a lot, even during while I'm recording, I'll type an incorrect variable.

  • As soon as I moved to the lower line, I see that didn't move to uppercase.

  • So I know right away something is wrong with my variable.

  • So that's a great way to understand clearly how to properly name variables.

  • We want to make sure that we're actually naming that properly.

  • Now, whatever way you use, it's more of a personal effort.

  • I've seen people use things like a lowercase long as variable.

  • So however you want to remember your variables is fine, but as long as it's very clear to you, and that when you return back to your code, you quickly, very quickly understand the purpose of those variables and how they fit within your macro and how those macros fit within your application.

  • So notice here, I've got customer row and customer column as long variables and customer name.

  • So everything is within a customer add new or customer saver update macro, which is within a module called customer macros.

  • So everything is modular and everything fits together properly.

  • It makes reading your code and understanding your code a lot easier.

  • So naming is a critical component.

  • We also use named ranges inside our code, and that can be helpful too.

  • Another thing we want to make sure of, and let's just say, for example, we have application where we've got, let's say a customer list here and we go into the formulas name manager.

  • And let's say I have a, let's go ahead and cancel this.

  • I'm just going to create a new sheet real quickly.

  • And let's say we have a list of customer names here and I'll just put in customer names.

  • Fred will make his appearance and Lisa, and let's see Mary.

  • So I've got a list of customer names.

  • Now what we want to make sure of also is our named ranges that we possibly will use inside the code should also be named appropriately.

  • So for example, if I create a brand new one, so let's say customer, and then we'll do names.

  • Once again, we may have different variables under customer, let's say customer names or customer balances or customer address, different name range.

  • So we want to use that same modular type of programming, and then we can write a named range.

  • So it's something like offset.

  • And then we'll start at the header row.

  • We'll move one row down.

  • So that's just in case we don't have any data, no columns over, we're going to use count a.

  • And then what we're going to do is again, using the header row and a large row, we're using the header row in case there's no data.

  • We want to make sure.

  • I need to subtract one for the header row, a single column, and we're going to use the tab key out.

  • And we're going to use the shift tab key.

  • We're going to make sure that it's dancing outs around those.

  • So now I've got a name called customer names.

  • If I edit that, I can copy it using control C. Now, if I want to refer to that inside the code for any reason, I can do something like this.

  • We can dimension customer range, let's do name range as a range.

  • And then we can also set the customer name range equal to, and then we can use brackets for that named range and we can paste it in there, which is customer names.

  • So here we've got a named range.

  • Now what we do want to make sure of is that we don't match the name.

  • In other words, our named range called customer names should not match exactly any variable referring to that.

  • We want to keep them very, very different and very separate.

  • So here, what we've done is we've again created a dynamic named range and we're referring it to within the code.

  • So that can be extremely helpful when we have clear names for one, our variables, clear names for our macros, clear names for our named ranges and clear names for our modules.

  • So naming is a critical component of any VBA application.

  • Next up, when we're writing code, we want to actually write it in a way that's readable for us and other people that might be looking at our code.

  • And we do that with proper spacing and indentation.

  • So the best way to do that is when we're using for next loops or we're using any kind of a loop or if then statements, we want to make sure to indent properly.

  • Notice that this is going to make it clear that anything we write in here is inside that for next loop.

  • So no matter what, let's just do customer row.

  • We can do something like sheet one dot range a and customer row dot value equals customer row.

  • So in this code, we see that everything we write was within that.

  • Now, if we do an if then statement, if customer row is less than five, then so notice now, once we're in the if then statement, we also want to indent everything within there.

  • So that is clearly stated.

  • Let me go ahead and show you a little bit larger project that I did so we can see how that would be on a larger project.

  • So I'm going to open up one of my recent projects.

  • Let's just do this one, this ERP software here.

  • So this is a pretty good one.

  • And we can see some of the way that we do the code inside these larger applications.

  • Once this code opens up, now we see here inside this large code, we have lots of different sheets and those sheets also must be named too.

  • And inside these individual codes, let's go ahead and pull in one of those codes here.

  • We can take a look.

  • So we've also got a list of modules and we notice that each individual module, customer macros, employee macros, logging out, everything is clearly labeled inside the customer macros.

  • We can see that inside a macro, we also have proper indentation.

  • Here we have inside the width, we've moved it over.

  • We have inside our customer width, we have a width statement.

  • Inside there, we have a for next.

  • So everything has been properly indentated.

  • We also have, again, clear macro names.

  • And another thing that I wanted to share with you is the ability to have sheet code names.

  • We'll take a look inside here and we see not only do we have the sheet names such as admin chart data customer, we also have a sheet code name.

  • And that's a very, very helpful to when we're referring.

  • Let's go back inside our original sample, which is right here.

  • Now maybe we want sheet one as a customer sheet.

  • We also want to make sure that inside that application, we clearly label the sheet name so we can do it from here.

  • Customer if it's customers or whatever it is, customers, or maybe we'll just do customer form, make it a little clearer.

  • So the more clear now sheet names, we can use characters such as spacing and so on and so forth.

  • So that will help us identify the specific sheet.

  • But when we're inside the coding, we really want to also change the sheet code name.

  • So let's go ahead and take a look.

  • We're going to go into the properties here.

  • And we have both the sheet code name, which is currently sheet one.

  • And we have the sheet name, which is called customer form.

  • So the sheet code name, again, we can use a short but descriptive name for that.

  • And again, once again, no spaces or special characters allowed.

  • Now why do we want to change this?

  • We want to change it for several reasons.

  • One, we can clearly see inside the code what our sheet does.

  • So we can refer to it inside the code.

  • So for example, instead of sheet one, let's clear this up.

  • We may want to use something like customer.

  • So I can then use customer form dot.

  • And when I use the IntelliSense dot here, it's going to come up and that tells me I've got the right sheet name.

  • If I use the wrong one and I hit dot, it doesn't come up.

  • So being able to use those sheet code names within our code is not only helpful because

  • I know exactly what the sheet doesn't say sheet one.

  • If it says sheet one, I don't know what sheet one does.

  • If it says customer form, I know that exactly the purpose of that.

  • So naming those sheet code names can be helpful as well.

  • And we see inside our larger project that we also have named both the sheet names and the code names in each instance.

  • So we see here each one, customer database, customers, chart data, admin, everything has both the sheet name and a sheet code name.

  • And again, once again, we see that everything is properly indented and properly spaced.

  • So that is also very helpful.

  • One of the also most important aspects of coding is being able to comment out and commenting is simply adding an apostrophe and letting us know what a specific range does.

  • So for example, let's say this one, we're going to say you will use an apostrophe set customer named range.

  • So comments are both useful for you or anybody else that can view the code to remind us exactly what the code does.

  • So we could do something like for customer start customer loop.

  • So this is extremely helpful so that we understand exactly what's going on with the code at the time.

  • Commenting is one of the most important and most common ways to clearly identify exactly what the code is doing at the time.

  • And like I said, even if nobody is going to see your code, it is helpful for you and yourself if you come back to your code over a period of time to understand exactly what the code is doing and will help you find errors and understand about the application development.

  • So it's very, very important to do that.

  • Now we can use commenting at the end of a specific code or we can use commenting in its own line and we can use as many apostrophes as we want.

  • We can use capital letters, start loop.

  • And also we can see that the colors here of that are in green.

  • However, of course you can change the colors.

  • You can change anything you want.

  • We see the dimensions are in a dark blue.

  • We can do that using our tools here.

  • And then we have some options here.

  • If we go into the options, we go into the editor format.

  • And from here you can change the colors of anything else.

  • So we see the comment text is green.

  • If I were to change that to a different color and then click okay, we see that it's in pink.

  • So we can automatically change the color however we want using the options.

  • So that's kind of a handy way to automatically set things up exactly the way that you might want to see it.

  • And it's going to help you identify and understand the code.

  • Also making for clear and speedy development inside your code.

  • So it can be quite helpful.

  • Okay.

  • We'll return that back to the way it was.

  • So commenting and changing the look and feel of it.

  • Of course, if you want a larger size, you can also do that too.

  • So we can also change the font size.

  • If it's too small for you or you want a different font, once again, we can go into the editor format here.

  • And if we have our normal, we can change the size.

  • We can change the font.

  • So keep that in mind that we do have that ability for very clear code that you can understand and help review.

  • Another way to automate and speed up your code development and also increase accuracy is to use an automator, a code automator, or something like AutoHotKey that automatically allows you to type in code.

  • For example, if you've seen any of my videos and you've seen me type code like this really, really fast, something like that, or something you see me do with sort here like this, notice how quickly it types out.

  • What I use is something called AutoHotKey to automate my typing.

  • And I've got a few videos on that, including the VBA Code Automator.

  • I've also got a few applications that I sell that incorporate this.

  • And so what this uses is a script.

  • For example, let's take a look at this script.

  • This is actually called Code Automator Script.

  • It runs with AutoHotKey.

  • Like I said, I got dedicated videos on this and we can create and automate anything.

  • So for example, if I do double colons and I do TTT, and then we can do something like double colons one more time.

  • Maybe we want to do if X is greater than Y, then X equals S or something like that.

  • Whatever you want.

  • I was just talking about variables.

  • All right.

  • So let's do customer row is greater than 10, then customer row equals 10.

  • Okay.

  • So whatever you want to do, then let's type this in correctly.

  • Make sure that if you want to use enter or new line, you can do something like this.

  • Enter like that.

  • Make sure you close the brackets up.

  • So that is also important as you can see it here.

  • Also what we want to do then is then save our script.

  • So we'll go ahead and save it.

  • And I also want to reload the script.

  • That's an option on the AutoHotKey.

  • So once it is reloaded, you can then use that inside your code.

  • So for example, if I do inside my code TTT, let's do not capitals, TTT, you see that it automatically types out.

  • So that's a great way to save time when you're coding, especially when you're doing common coding things.

  • Now I've got a video on that.

  • I've also got an application called the Ultimate Developers VBAs.

  • We can see it here.

  • Let's take a quick look at that.

  • And here it is right here.

  • In this application that I sell includes over 500 macros.

  • We can assign a shortcut.

  • So if maybe we want to print the active worksheet, we can do something like PA print active worksheet, create a shortcut on that.

  • Once we save this code, it is automatically going to be available very quickly inside our, so we just do PACT and it's automatically going to type out.

  • That is called the Ultimate Developers VBA library.

  • I've got over 500 macros.

  • I'll include the link down below in case you want to have a look at that.

  • All right.

  • So those are some great ways to automate coding and that's going to help you save time also to increase accuracy and also optimize the code using the auto hotkey.

  • So that's something that I use all the time.

  • Let's take a look back inside our code and go over some more samples.

  • We can also use error checking to help us out.

  • Now that's really important if we want to understand error checking along with debug.

  • If we want to understand what's going on inside our code, we can do that too.

  • Let's say we're going to loop through the customers one through 10 and we want to see what's going on inside.

  • We can use the immediate window.

  • I've got a training dedicated on that so we can do something like debug dot print and then customer row.

  • Once we put that in there, we're going to look in view and then we're going to look in the immediate window.

  • We can also use control G and what that's going to do is going to launch the immediate window and mine's a bit down here.

  • There it is.

  • Let's just bring it up.

  • I'll clear whatever's inside here.

  • And so what we want to do now is we can now run the macro.

  • So we don't need this.

  • We can comment this out if we want to comment it out.

  • And so here what we're going to do is we're going to run our loop.

  • If it's less than five, then we want to show that customer row within the immediate window.

  • We can run it here, run it completely.

  • And you see now that inside that has appeared.

  • So the immediate window is a great way to understand what's going on with our code and it's going to really help optimize it.

  • So we know exactly step-by-step if we want to use F8, F8 is the step through.

  • It is also this one right here.

  • We can see this step into and we can automatically see how it moves through the loop.

  • So understanding this is really going to help us optimize and it's a great practice to use the debug so that we can understand what's going on with our code.

  • We can also use error checking inside that to see if there's an issue and error checking will really help us understand without creating bugs.

  • So how would we use some error checking in that to help us understand?

  • So let's say we're going to look for a customer name inside the customer range.

  • We can remove this for now.

  • And then what we can do is I want to go inside and we'll set a customer.

  • So the customer name is equal to let's do Fred Fretters who he makes an appearance here.

  • First we will want to set our variable set customer name, right?

  • You will customer names.

  • Customer name is Fred Fretters.

  • Now we're going to look for that inside that.

  • So customer named range dot I want to use the find on what am I looking for?

  • I'm looking for the customer name.

  • There's a few ways to do that.

  • I'm going to look in Excel values and Excel whole and then what we're going to use is dot row.

  • I want to return the row.

  • Let's go ahead and make sure there's no error first, but I'm just going to return Fred here and we're going to look for that.

  • We'll clear this out and then what I want to do is debug print in the customer row.

  • So we're going to set the customer row is equal to whatever row it's found on.

  • We will do debug dot print and then customer row.

  • So we're going to run this here and we see that it's returned four.

  • So we can see that the customer row has been returned on row four.

  • But what if it is not found?

  • If we put in something like Fred Fretters, it's not going to be found and it's going to return an error.

  • And I really don't want it to return an error when it's not found.

  • So let's take a look at this.

  • So we see that it returns the air object variable or with block variable not set.

  • So we can't really have that.

  • So we see that it ends up on this line.

  • So how do we deal with that?

  • Well, we can write something like on error, go to, and then just do something like not found.

  • And what that's going to do is going to skip our debug and we can exit the sub out if everything performs correctly, exit sub.

  • However, if it does not, we can do something like not found, which is where it's going to skip to.

  • And we can put something like message or we could do traditionally, this would be a message box.

  • So we'll do message box, customer not found.

  • Very good.

  • So we can now reset that.

  • We're going to run the same code and we see it tells us the customer has not been found.

  • So we can use on error, go to, and our skip to, to let the user know without actually incurring a bug.

  • So that can be very helpful.

  • There's another way to do that without the named range.

  • We can use something like this.

  • We've already renamed our sheet.

  • Let's take a look at our sheet.

  • Our sheet is called customer list.

  • So we can use something like customer list dot range, customer names, and then we can refer to that.

  • And that I also like to use a lot.

  • So again, we see a customer not found as it's going to work.

  • However, let's go ahead and make sure it is found to make sure that it does work that way as well.

  • So we can clear the name out.

  • We can then run this macro and we see that has been found on row four.

  • Let's clear that out one more time, just so you can see that it has been found on row four.

  • Very good.

  • We can use on air, go to, and then not found to skip issues that might come up and also let the user know exactly what type of an issue or such as a not found within a range.

  • Very good.

  • So we have been over a lot.

  • Let's take a quick look inside my existing code and some of the modules to see some more information about how we can use code.

  • So here we see that I've used commenting both above and to the right of the line.

  • So that can be helpful.

  • We're also using indentation, which is going to help us.

  • We actually used naming both in the macro name in the module name and the sheet code name and the sheet to help us understand exactly what's going on in our coding.

  • We can use named ranges both in side brackets, which can help us understand.

  • We also want to make sure that our named ranges such as shared folder is not the same name as our work variable.

  • So we want to make sure that those are different.

  • We also use a combination of capital letters and lowercase letters in very descriptive variables so that we understand exactly the purpose of that variable.

  • We also use capitals inside our macro names in order to also understand and help us quickly know exactly what's going on inside the macro.

  • So all those namings really help the indentation really helps.

  • And so if we combine all these things, we have very clear, very accurate, good list of best practices.

  • Now, I've included a cheat sheet here.

  • If you want that for all the VBA best practices to have, most of which we have been over.

  • All right.

  • Well, thank you very much.

  • I do appreciate your continued support right here on YouTube or wherever you are.

  • If you do want additional help, I've got some great links down below.

  • Don't forget to join our Excel for Freelancers Facebook group.

  • We have over 65,000 members in that group.

  • And there's even more help and more videos, more content on Patreon.

  • So make sure to join us there.

  • Thank you so much.

Developing applications in Excel VBA is often full of challenges and frustrations, but it doesn't have to be that way.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it