Excel 2019 VBA Beginner Tutorial (Part 1 of 4)

Excel 2019 VBA Beginner Tutorial (Part 1 of 4) Hi everyone i'm trish connor cato welcome to the excel 2019 visual basic for applications video course this course is for beginning users looking to automate repetitive and recurring tasks in microsoft excel vba is microsoft's programming language and it's built into the office applications our focus during this course is on excel specifically you'll be equipped with the basics to start writing your own vba code modify the code behind macros you've already recorded.

And have an understanding of how vba lends itself to creating efficiency in your daily tasks we'll start with the basics where you'll learn the types of things you can do with vba versus recording macros in excel as well as some key terminology which will help in your vba journey and you will get to edit vba code you'll learn the importance of macro enabled workbooks and how to save them as well as how to modify some security settings we'll move on to understanding the excel object model.

Collections and how to reference objects in vba code once these basics are covered you'll be ready to move on learning about the different types of procedures creating procedures learning the scope of procedures and working with methods you'll also learn how to get vba context-specific help from within the visual basic editor if you're enjoying these videos please like and subscribe if you want to earn certificates and digital badges please become a member of our patreon.

The link is in our video description if you have any questions you want answered by one of our instructors please join our off-site community the link is in the description as well as always if this course has exercise files you'll find them in the video description below in our first lesson getting started you're going to be learning about an overview of vba that includes what type of language it is why you would want to use visual basic for applications.

Versus recording macros and advantages that it can give you in terms of efficiency we'll move into recording a macro and running it in excel and then we'll go into the visual basic editor environment and you'll get an overview of the environment before we start editing a macro in vba you'll learn how to save a macro enabled workbook and the importance of doing so you'll learn a little bit about macro security now in this lesson we're going to be using two files.

That are in the video description there both excel files one is named vehicles and the other one vehicles two we will be creating a vehicle's macro enabled workbook during this lesson and the name of that workbook is shown on the slide you will not find it in the video description at this time so what you would want to do is grab those two excel files vehicles and vehicles too from the video description and put them somewhere on your system where you have easy access to them.

Excel 2019 VBA Beginner Tutorial (Part 1 of 4)

Before we get hands-on let's go ahead and get an overview of vba so as mentioned in the introductions vba is a microsoft programming language that is currently built into the excel word powerpoint outlook and access applications it's one of the many programming languages that evolved from the basic programming language which was developed in the 1960s it was first released in excel 5 and that was in the office 1995 suite since then it evolved to encompass.

The applications that are mentioned in this slide vba is known as an object-oriented programming language oop this means that everything within an application is an object including the application itself objects have their own set of features and uses known as properties and methods respectively here's an example of object oriented programming so the example using excel would be that excel is an object that contains other objects for example cells worksheets charts pivot.

Tables shapes etc each object has its own properties for example a worksheet has a name a workbook can be opened and closed and these are a couple of a workbook's so methods are like actions you will work with objects and manipulate them via their properties and methods throughout this course so why vba it can give you the ability to run macros automatically create user defined functions which can be used in the excel application.

You can use vba to control other office applications and mostly to automate recurring and repetitive tasks don't get me wrong the power of macros is excellent in excel but vba does have some advantages so for example there are no limitations when you're recording macros you are limited to those tasks that you can perform in the excel interface with vba you can attach code to events so that it runs automatically when the event occurs for example activating a worksheet.

Would be an event vba allows for decision making and it has several decision-making structures ensuring code only runs when certain conditions are met the looping structures in vba ensure code runs multiple times based on a condition and then there's forms and boxes you can use vba to create user input forms dialog boxes and message boxes in excel so we're going to get started i have the vehicles excel file open from the video description.

And we are going to be recording a macro in this file now the thing is we want to prepare excel for recording a macro there are several ways that you can start a macro recording but we want to add the developer tab to the ribbon as that is one of the ways that you can record your macros review your macros so on and so forth so the first thing we're going to do is right click on any ribbon tab home insert page layout whatever.

Posts Related:

    And when you right click on a ribbon tab you're going to choose customize the ribbon

    In the customize the ribbon options box on the right side everything that has a check mark is a tab that's showing on your ribbon we want to check the box in front of developer and then at the bottom you're going to click ok and now you have the developer tab on your ribbon and let's navigate to that tab so for parts of this course we're going to be using this make vehicle information so we.

    Have make-believe then numbers we have year we have make model classification color dealer cost and manufacturer suggested retail price columns in here and we're going to record a macro that is going to apply the column headings and also a little bit of formatting so on the developer tab of the ribbon in the first group the code group you're going to click on the record macro button and don't worry it's not going to be recording until after we clear the dialog box so.

    When you click that button the record macro dialog box opens and you have to give your macro a name well you don't want your macros to have generic names so make them as descriptive as possible now of course there are rules to naming macros macro names must begin with a letter can be alphanumeric and can contain the underscore character no spaces so we're going to name this macro capital a add capital f formatting almost together so it follows the naming convention we're not going to assign a.

    Shortcut key to the macro to make it run later we don't need to do that here and you have choices as to where the macro is going to be stored so by default it's going to just be in this workbook the file that it's going to be recorded in this workbook your other choices are a new workbook or your personal macro workbook if you store it in the personal macro workbook that workbook opens in the background every time you open excel and so the macro is available to any and all excel.

    Files we're going to leave it on this workbook and there's another way that you can access it from another file that you'll see a little bit later i like to add a description to a macro so that if anyone comes behind me and they need information about what the macro is going to do the description will cover that so we're going to type in a description box add column headers comma bold and centered period apply accounting.

    Format to column g period autofit columns a through h so that is the intent of the macro that we're going to record so at this point once we click ok everything we do until we stop the recording is going to be recorded and i like to say this while you're recording the macro if you make a mistake and you correct the mistake you don't have to start all over again because it's recording both the mistake and the correction so go ahead and click okay.

    You'll notice on the developer tab now where it used to say record macro we now have our stop recording icon there and we'll use that when we're completely done going through all the steps that we're saving in our macro so for right now the first thing that we want to do here is right click on row heading 1 and choose insert so we get a blank row a new row one and that's the row that we're going to be adding our column headers in and so we're going to click in cell a1 and we're going to type then v i n and i'm doing it in all.

    Capital letters so vehicle identification number i'm going to press my tab key to get over to b1 and i'm going to type year tab make tab model tab again classification the next one is color the next one is dealer cost two separate words and lastly in all caps msrp for manufacturers suggested retail price after that one i'm going to just press enter so we have our headers in where they need to be and then we're going to select row heading 1 again.

    And we're going to go to the home tab of the ribbon in the font group we're going to make it bold and in the paragraph group we're going to use the center alignment button so the headers are centered within their cells now we're also going to add in adjusting column width in here so don't worry about that right now the next thing we're going to do is we're going to select column g by clicking on its column heading so the entire column is selected and on the home tab in the number group we're going to click on.

    The dollar sign for the accounting number format and then the last thing we're going to do

    Is we're going to select column headings a through h and i just clicked on a and dragged across and we want to auto fit these columns with right so that's what we want to do we want to auto fit and we do that by going to the cells group over to the right on the home tab of the ribbon and in the cells group you're going to select the format drop down and from that drop down.

    You're going to select auto fit column width so now every column is wide enough to display everything in the column and if we were to make entries and they were wider than what they are now it would automatically adjust the column width now typically when i'm done recording my macro before i click stop recording i just like to click on any blank cell just to make sure that nothing is selected we're going to go back to the developer tab and choose stop recording in the code group.

    Typically you cannot undo the effects of a macro so we just recorded this macro while we were recording it we were doing the steps now what i'd like you to do is i'd like you to press ctrl z which is undo and the only thing it undid was the column auto with everything else our headings are there they're bold and centered we have our accounting format and column g and it just undid the column autofit so we want to redo that step so we're going to just do control y which is redo.

    And we end up again with the result of our macro you will learn some workarounds for undoing the effects of a macro later on but for right now we want to test this macro on a different sheet so we're on the inventory sheet tab in this file we have two other sheets let's go to sheet two sheet two has the same data that was on sheet one initially when we came in here and this is a good sheet to test our macro on so we can go on the developer tab in the code group.

    This is where you can use the macros button to access any macros that you may have in this file so we're going to click on macros and we only have one ad formatting it's already selected and on the right side you're going to click the run button so at the end of the day you should have those column headings they should be centered and bolded and the auto width the column auto width is in effect and you have the accounting number format in column g.

    So while we were recording our macro it was generating visual basic code in the background visual basic for applications code in the background now on the developer tab we can get to visual basic you will learn other ways of starting macro recording without using the developer tab you'll learn shortcuts throughout the course on how to switch back and forth between visual basic for applications and the excel interface but for right now on the developer tab the first button in the code group is visual basic let's go ahead and click on it.

    Visual basic for application opens in its own separate window and so you literally have this window and your excel window open at the same time and we're just going to focus on the vba window right now and so before we get started in here we're ultimately going to edit the code behind the macro we just recorded but i just want you to get acclimated to this environment this is known as the visual basic editor or vbe just so you know it doesn't have a ribbon interface it never updated.

    It still has the old school menu bar where you have file edit view and everything and then underneath it it has some tool bars that we use this was like pre-ribbon days how excel itself and all the other office applications used to be on the left side of your screen you may have two different panels open you have a project explorer window at the top and a properties window at the bottom if you don't have both of those panes showing you can go.

    Up to the view menu and you can click on project explorer and then come back to view and click on properties window and you can see the shortcut keys for both of them or you could do ctrl r to bring up project explorer and f4 to bring up the properties window let's talk about the project explorer window first for every excel file that you have open you will have a separate project so right now i only have one excel file open it's the vehicles dot xlsx file so it creates.

    Its own vba project and it has the name of the file afterwards now i may have a few things here because i have some add-ins that create projects as well if you don't have them you're fine but this is a cautionary tale here because there have been times when i've had like five excel files open and i find myself doing some coding and then i look and see that i'm doing it in the wrong files project so i can always cut and paste it to the right project but you do want to.

    Be aware of what project you're working in when you click on vba project vehicles.xlsx it expands and you can look up at the title bar and it lets you know that it is in that project now so just kind of get in the habit of making sure you're in the right project it has two folders in that project microsoft excel objects those are your sheet tabs so we had the inventory sheet we had a sheet 2 and a sheet 3 and then the entire workbook is an object as well so.

    It lists that then you have another folder called modules that folder was created when we started recording our macro actually when we finished recording our macro it created that modules folder your code is stored in modules so expand the modules folder and it created a default module it names it module one and if we were to go back over to excel now and record another macro it would also put it in module one if we were to close excel saving it properly and then reopen.

    It and start recording a macro it would create module two it would always give it the next number module two module three you can also rename modules in here which you'll see in a little bit so if you click on module 1 you will actually see the code window and that is everything that it created while we were recording our macro so it converted our steps into vba code and we're going to edit this in just a little bit but i want you.

    To have your editor window be as comfortable an environment for you to be working in so one of the things we're going to do is we're going to go up to the tools menu and click on options we will revisit some of these settings later in the course but when you go into options you have four tabs editor editor format general and docking one thing one setting that i want to make sure that you have checked well like make sure all of the code settings have.

    A check mark in front of them and as we work in the course i'll explain what these settings mean your windows settings you can actually have all of those checked as well go to the editor format tab and this is where you can set your font size so i have my font set to 14 point which is comfortable for me i can bump it up to 16 and then at the bottom i'm going to click ok so font size and if you want to go back in and change the font i'm cool with the code looking font courier font.

    Um that works for me i'm just so comfortable with it it doesn't bother me so i just wanted you to know that you can change some of those settings in the environment for yourself and for the ones that we are going to be utilizing in the course we'll go back in there and talk about what those settings are actually doing the other thing we want to customize before we edit our code is we want to add three icons to the toolbar that we'll be using frequently throughout this course so what i'm going to do is.

    I'm going to just right click in a blank area of any toolbar and go to customize and the customize dialog box opens you want to make sure you're on the commands tab at the top and then you'll see a list of categories that mimic the menus that are up there file edit view insert so basically you're gonna have to tell it where it resides on the menu in order to add it to your toolbar so under categories we're gonna select edit and then all the commands from the edit menu show.

    On the right side of your screen and on the right side of your screen you're gonna scroll down we're looking for two commands the first one is comment block and the second is uncomment block so what i'm going to do when i find comment block i'm going to click and hold on it and i'm going to drag it right after the question mark on my toolbar and then i'm going to grab uncomment block and drag it right after comment block now we have one more command that we're going to add up there.

    Under categories we're going to click on debug and the command at the top of the list is compile project i'm going to grab that and drag it right after uncomment block so i have comment block uncomment block and compile project when we get ready to use those commands i will explain what they're going to do for us they're going to be kind of helpers for you let's examine the code that was generated while we were recording our macro first of all let's.

    Look at the information starting from the top we'll talk about the option explicit statement a little bit later in the course don't worry about that right now what i want you to focus on is the name of our code ad formatting it comes after the word sub and you'll notice that both option explicit and sub are in blue that means those are visual basic for application keywords they mean a specific thing in this code and should only be used for their intended purpose it creates.

    What's called a sub procedure to contain our code so it starts with the sub statement and if you scroll down at the very bottom it concludes with an end sub statement you have to have both if you have a sub and not an n sub you're going to get an error message but it did this automatically while we were recording our macro it gave it the name that we named it ad formatting and then after it it put a set of parentheses some procedures have parameters that need to be defined and that would happen inside the parentheses.

    We're not there yet but that's why they're there and they need to be there then you'll notice several lines that are in green beginning with apostrophes those are comment lines so when we were filling out the record macro dialog box right we named it ad formatting and we put a description in add column headers bold and centered apply accounting format blah blah blah comments are meant to describe your code it's a good idea.

    To comment your code liberally i've written code before and didn't comment it and six months later i had to figure out what the intent of the code that i wrote was so comments are really good and so when you put a description in the record macro box it comes in as comments they're informational when you run your code or execute your code the comments do not execute and we can do a little bit of clean up there um we don't need that blank apostrophe at the top so i'm going to just.

    Delete it and then i'm going to delete the two underneath because they're not necessary either and then it starts our code well the first thing we did is we right clicked on row one and inserted a new row so that's rows one colon one dot select and then selection dot insert right and so it moved everything it shifted everything down and gave us the blank row at the top and then we clicked in cell a1 and we typed it in so that's range even though it's a cell.

    Range a1 dot select notice the quote the double quotes inside the parentheses there surrounding a1 and an active cell which is now cell a1 don't worry about formula r1c1 right now but activecell.formular1c1 equals whatever we typed in that cell then b1 c1 d1 e1 f1 g1 h1 same thing and then we selected row one again so at this point we're all the way down here in the code when we selected row one again and once we selected it we made it bold so before that.

    Selection dot font dot bold would equal false when we made it bold it changed the false to true and then you have what's known as a with end with block starts with the word with and if you go down several rows it ends with an end with statement and this is an interesting thing you'll learn more it's known as a construct right you'll learn more about this construct as we go in the course but this allows you anything that you do in the paragraph group on the home.

    Tab is included in this width block bold is not in the paragraph group on the excel ribbon right but your alignments are the ability to wrap text your orientation whether you're going to indent merging cells all of that is included in the paragraph group on the home tab in excel so all of these settings here are those things that mimic the paragraph group and what we did is we centered those headings so what happened is when we did that the dot.

    Horizontal alignment equals excel center it was changed from excel left because typically things are left aligned in a cell so all of these other settings are the default settings the only thing that changed was excel center one of the things you you want in your code is for it to be as concise as possible we don't need everything that's in that with block we we really would just keep the horizontal and vertical alignment but we didn't do any wrap text orientation indenting.

    Any of that kind of stuff so we're going to select the lines from wrap text all the way down to the dot merge cells equals false statement we need to leave that end with statement underneath that so once you have dot wrap text through dot merge cells line selected go ahead and press delete and then i'm a little ocd so i'm gonna shift tab to get the end with statement back in line with the with statement so if i have to troubleshoot code.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continuetomake videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=WZnRDPFz75U
Previous Post Next Post