VBA Training Series – How to Write Your First Macro (Part 1 of 3)

Macros & VBA Training Series Part 1:

Writing Your First Macro & The Excel Object Model

Double-click video to view in Full Screen HD.

Download the file used in this video:

My-First-Macro-Reference-Common-Objects.zip

Macros & VBA Training Series

Enroll for FREE

The video above is the first in a 3-part training series. Can I send you the next two videos?

Please click the link below to enroll in the free training. I will send you video #2 in just a few days.

Please click the big green button above to register for the free training series.

Jon Acampora Circle MVP Profile 2019

About Me

Welcome to Excel Campus!  I am excited you are here!

My name is Jon Acampora and my goal is to help you learn Excel to save time with your job and advance in your career.  I've been an avid Excel user and VBA developer for 10+ years.  I am also a Microsoft MVP.  When I'm not looking at spreadsheets, I get outdoors and surf. 🙂

What Process Do You Want To Automate in Excel?

Please leave a comment below with your answer, and any questions.  Thanks!

1,683 comments

1 8 9 10 11 12 23

Your email address will not be published. Required fields are marked *

  • Sorry if this is asked/answered, above…
    Would love to have the worksheet/file I’m currently working on open automatically upon first time I open Excel.

    Thx!
    Les

  • Hi Jon, I just watched your first video. I am a beginner, pretty much, in Excel.I am going to see if I can pick up some of this trough your videos but I have a minor handicap. I am deaf and I was wondering if you have any kind of printed instructions I could get to go with each video/ Thanks for a great video.

    • Hi Gary,
      Great question! I don’t have printed instructions right now. However, you can access the video transcripts on YouTube. You can also turn the closed captioning on. Here is a link to the video on Youtube.

      https://www.youtube.com/watch?v=wRyGNAmbNSg

      The transcript can be accessed by clicking the More button under the video title, then choosing Transcript. I hope that helps. Let me know if you have any questions. Thanks!

  • Always wanted to learn excel macro’s but never had the time. I am now retired so I have the time and the first video was well done. Looks promising that I will be able to understand them and be able to write them.

  • Hi Jon, I have a workbook that includes about 20 worksheets. It’s a quoting spreadsheet and I may only use 3 – 4 of the worksheets. I would like to write a macro that would delete all unused worksheets before I save to my computer. Can you help?

    • Hi Dave,
      To delete worksheets we can use the Worksheets().Delete method. To delete multiple worksheets we just have to loop through the worksheets, determine if it need to be deleted with an If statement based on some criteria, then delete the sheet.

      I explain more about how to loop through worksheets in video #2. That video will be out next week. Let me know if you have any questions after that video. Thanks again!

  • Hi Jon,

    Your first video was so fascinating. I can’t wait to learn all your videos. I am very interested to use VBA in Financial Modelling.

    Looking forward to learning from your videos.

    Cheers,
    David

    • Thanks David! I really appreciate the nice feedback and happy to hear you are learning VBA. Awesome! 🙂

  • I am looking forward to learning more about macros and VBA. I took Excel in school and use it daily on my job. One of our biggest uses is Excel pulls files from our data base and we use them for different things I would like to lear more on how that works. Thank you for sharing.

    • Thanks Sheryle! I’m happy to hear I’m not the only one using Excel daily… 😉 It’s one amazing tool.

  • Great stuff for beginners, like me.

    Nice presentation with no jargons.

    Look forward to second & third videos!

  • Great Video and direct to the point ! Thanks ! Process I want to automate is to print the excel spreadsheets to pdf without request print filename user intervention.

    ExecuteExcel4Macro “PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)”
    Sheets(“Sheet2”).Select
    ExecuteExcel4Macro “PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)”
    Sheets(“Sheet3”).Select

  • Hi Mr.Jon, It is an Excellent video. Very good explanation.! Thank you very much and awaiting for remaining videos…

  • Hi Jon
    Great video. I’m an absolute beginner in the VBA area and I look forward to watching the next videos.
    I’m currently following Myndas Power Query/Power Pivot/Dashboard course (also fantastic) and using a lot of what I learn in my daily job. I have no specific VBA projects in mind, but I’m sure I will have after watching your videos.
    Thank you for sharing your skills in this great way.

    • Thanks Marianne! VBA is great for automating tasks that you do over and over again, that take a lot of your time.

    • Hi Elly,

      Formulas and pivot tables are a bit more advanced, but once you understand the core concepts it’s not too difficult to learn. The nice part is that just about every aspect of pivot tables can be programmed with VBA. The same is true with formulas. Please let me know if you have any other questions after the next videos. Thanks again!

  • I have seen many tutorials on VBA & Macros. I think this the best. The explanation is clear.
    It’s awesome.

  • A thoroughly enjoyable introduction video making starting in vba clearer than some documentation I have read. Please keep the simple straight forward approach (assuming that you audience do not know vba) for all future videos.

    On a practical point a common requirement in excel is to compare data from one sheet of data to another to identify discrepancies (possibly from one month to the next). This can be achieved with vlookups but it would be useful to have an automated application that could deal with differing number of rows of data from one month to the next

    • Thank you Jim! I’m happy to hear you enjoy the videos. That is a great suggestion for a future tutorial, and a very common task.

  • Thank you Jon, clear and easy video for beginners. looking forward to your 2 and 3. also appreciate links in comments to other useful material related to specific topics.

  • Thank you for sharing your knowledge with FREE training. I have been wanting to learn how to write macros for a while now, but I haven’t felt confident enough about being able to remember everything. I hope your video tutorials will help teach the information in a way that helps retain the information. I have a couple of initial things I would like to do with macros. I have a worksheet that draws data from an SQL server database into and Excel worksheet. I have the query refresh when the file is opened, and there is a pivot table that summarizes the source data. I want to write a macro that will refresh the pivot table once the data refresh is complete. I would also like to learn how to create what might be a user form, that prompts the user for information, and based on that information draws data from an attached SQL query. I have a lot of workbooks with SQL queries, but the updating of the summary information is manual.

    • Hi Julia,
      Yes, that is all possible with VBA. The pivot tables can be refreshed with the Workbooks.RefreshAll method. We discuss userform in the video #3. Thanks again! 🙂

      • I really appreciate you answering this as I have been trying to figure out how to update pivot tables as well. I have 2 follow-up questions though: I want to refresh a single sheet within a workbook, is there a way to do that?

        Also, how do I run the function without going into the VBA editor?

        Thank you!

        • To refresh a single pivot table you can use the following code.

          Worksheets("Sheet1").PivotTables("PivotTable1").RefreshTable

          Just change the sheet name and pivot table name references.

          You can assign the macro to a button or a shape. I typically use shapes. Insert a rectangle shape into the sheet, then right-click it and select Assign Macro… You will then choose your macro name from a list. Now you can press the shape to run the macro.

          I have another video on different ways to run your macros. I will share it in the coming weeks as well. Thanks again!

  • We routinely receive spreadsheets that have to be “normalized” before we can import the data into other programs. Typically, that means removing leading “1’s”, odd characters (dashes, parenthesis, etc.). Also, we have to normalize dates/times and often set a custom date/time of dd/mm/yyyy h:mm:ss. And, we look for certain values in a particular column. If a line contains that value, we want to somehow highlight that line for further review. I nearly forgot, for some of our processes, we need to identify and isolate those lines which contains blanks in a certain column or field.

    • Sounds like a great process to automate Diane. VBA never forgets a step… 😉 On the other hand, I forget steps all the time…

  • Hello. I have a workbook which I need to update on a monthly basis and I need to update link files to the worksheet as well. I have tried creating macros to automate this process but they never run. This file includes regular tables, pivot tables, advance filters and a dashboard. I would appreciate it very much if you could help me. Thanks!

  • Thanks Jon for an exceptionally clear and concise introduction to VBA, I’m looking forward to the other videos.

    I want to be able to alter/generate charts.
    A simpler example:
    I have an ordered pivot bar chart and want to highlight certain values with a black fill (the highlights show threshold values within the data). I’ve managed to step through the series PracticeName and highlight the correct bars. However I have to address the start of the series directly in the VBA code. I’d like to be able to automatically search for the pivot field name “PracticeName” and return the cell reference as the starting point for me to loop downwards to find which bars to highlight black.
    A harder problems:
    I do a number of quadrant charts c.f. http://peltiertech.com/shaded-quadrant-excel-xy-scatter-chart/
    As they are presented by Peltier, they are quite fiddly and time consuming to generate. I’d like to speed up this process.

  • I am looking at this as a refresher as it’s been a few years. The tutorial is excellent. Very clear, very explanatory – I wish I’d had you as a teacher when I was first learning. I’m really looking forward to the next videos. Many thanks for this.

  • Thank you Jon for making this seem a lot less overwhelming to me. I’ve always been afraid to try this as I don’t really have a background in computer anything! I’m completely self taught by using videos such as yours. One of my co-workers is always telling me “we don’t do manual here” when I’m counting or copying/pasting, etc. in my reports so I thought I would give this a try. I just requested the rest of the videos so I thank you again. Maybe I’ll get the hang of it.

    • That’s great to hear. Thanks Ellen! VBA will definitely help take the “manual” out of a lot of processes. I didn’t start with any kind of computer background either, and just happy I can share what I’ve learned with others.

      Thanks again! 🙂

  • I really enjoyed the first video. I know a little about writing and editing macros in Excel but I only know enough to do very simple things. I have a project I would like to automate using vlookup command do you have videos that would cover that?

    • Hi Marie,

      I do have an article and video series on the VLOOKUP function. This is more about how to use VLOOKUP in the spreadsheet as a formula. I don’t have any specific videos on how to use VLOOKUP in VBA. But that is something I will consider for the future. Are you looking for VBA to create the vlookup formula?

  • Jon,
    Thank you for the clear start to MACROS. I am going to be automating a process that imports 2 separate spreadsheets(different lines of business) of data. Then calculate totals and percentages on these columns. Once this portion is completed they would like to have a consolidated spreadsheet created which would summarize by state and territory. Thanks for your help.

  • Hi Jon,

    Thanks for your class – the visualizations you have used are really helpful.

    When I ran the last line of code you wrote I got Run Time Error: ‘9’ Subscript out of range.
    I thought it might be because you had selected worksheet 1 but when I changed it to 2 I got the same message.
    I tried the help but it says its no longer available except outside the program.
    I’m still trying to get my head around the ‘error’ because everything you said made perfect sense to me.
    Can you help me out here?
    Thanks
    John

    • Hi John,
      I would be happy to help. Can you paste your code here, so I can take a look? Also, does your workbook have a Sheet1 or Sheet2? Sounds like there is a reference that does not exist in the workbook.

      Thanks

  • Thanks for sharing this Jon. I have heard about Macros, but have never tried them. I am trying to learn everything possible to make my job easier. I am a bit obsessed with learning Excel.

    • Hi Dan, Well that makes two of us… 🙂 And don’t worry, you and I will never run out of things to learn.

  • Hi My name is Krishan, I want to automate 2 process mainly , one is email sent attaching different sheets and another one is validation.

    1 – I do this practice monthly . I download the data from CRM software for approx 250 clients , segregate the data using macro and send them one by one as per their email domain to different clients. The email sending process i do manually.

    2 – After getting the data from client i validate them for our publisher team the numbers which approve i mark them approved in CRM software but before that i remove duplicates, I try to find the exact match then upload them in CRM software so it can be reflected in their dashboard. All the validation practice is done in excel manually .

    I want to automate both the above sheets.

    Regards,
    Krishan

    • Thanks Krishan! The email sending can definitely be automated with Outlook and VBA. Same with removing duplicates. I think you will be able to save a lot of time by learning VBA. 🙂

  • Love the Kitchen appliances analogy. I do some dabbling,but good to get back and understand VBA from the ground up. Looking forward to the next videos. BTW these are just the right length. Found you through Mynda’s MOTH.

    • Thanks Derek! I’m happy to hear you enjoyed the video. And tell Mynda I said thanks too. 🙂

  • Dear Experts
    I am using excel for my daily reporting. We use different worksheet for daily reports in same workbook.
    So I want avoid the any repeating data. Our data will vary row by row. Just want to have a info or warning while we type a same data in a new worksheet. It’s only two cells to be considered in a row.
    Example: wrksht 1: row 3. Cell A3 and b3 should not come again in New work sheet under any rows wrksht 10: row5 cell A5 and B5

  • Thank you, Jon. That wasn’t as scary as I thought. You made it simple, short, and informative. Great job! Especially useful for those of us who can only manage VBA in small doses!

    • Thanks Nancy! Learning VBA can definitely feel scary at first. There is a lot to learn and it can get overwhelming quick. I try to take a step-by-step approach to make it a bit more manageable to learn. I’m happy to hear you are learning VBA. 🙂

  • what is the difference between recording a macro as one undertakes the steps and writing in VBM and Then Come and Run The MACRO there after, or if there any pron and cons.

    • Hi Racheal,
      This is a great question! The macro recorder is a great tool that can help us get snippets of code to use in our macros. It does have limited capabilities though. In video #2 we are going to learn how to write loops. Loops are an amazing tool in VBA that can help you automate repetitive tasks efficiently. The macro recorder does NOT produce loops. It does not produce IF statements either.

      These coding principals and techniques can help us build fully automated systems and applications. The macro recorder is a great place to start, but we can take VBA a lot further by writing our own code.

      I hope that helps. I think you will see what I mean after watching video #2. Let me know if you have any questions. Thanks!

  • Hi Jon,

    I really enjoyed this video. It’s all pretty new to me, but I found it quite interesting and informative. I use Excel in it’s most basic form. I’m hoping video’s like this and additional training will help advance my knowledge and skills.

    Thanks,
    Jeanne

    • I’m happy to hear you are learning more about Excel. Thanks for the great feedback Jeanne! 🙂

  • Great video Jon. I started by cutting and pasting from the web, not really knowing the basics. This video was very informative and a great introduction to VBA. Thanks again.

    • Thanks Wayne! The web has a TON of great VBA resources and I use it everyday. It’s just great to know what you are getting, and how to change it to fit your needs. Thanks again! 🙂

  • Like the way you approach the subject.
    Have worked with VBA for some time, just wanted to see if there is anything that I missed.
    regards Heinzbert

  • I have a pivot table with data I update every week. I would like to create a macro to apply conditional formatting to blank cell(change cell color if cell is blank). Column is current Month and row is a user name.

  • Thank you for video 1 it was really clear and easy to follow. I have used the macro tool to generate macros but often forget to do one task and then have to start all over again to include that task. I can see now how easy it would be to add in a line of code and hopefully after your next videos I may even be able to start from scratch and write the whole thing myself.

    Where I have often struggled with macros in the past is when each month brings data with a different number of rows. It would be good to understand how to handle those please.

  • Hello Joh,

    I found your first video very clear and concise. It was easy to understand and your comparison to kitchen items made me smile! I have been demonstrating the Recorder for years (I am an Excel instructor). I go into the VBA editor to make minor changes but that is the limit of my VBA skills. I am looking forward to looking at the next couple of lessons at least! Thank you!

    • Thanks Donna! 🙂 I’m so happy to hear you are going beyond the macro recorder. The macro recorder is a fantastic tool, but it can only take us so far. In video #2 we will learn loops, which is something the macro recorder cannot produce. Loops are an amazingly powerful tool that really help us automate tasks. Thanks again!

  • Thank you for the videos Jon !! I’ve only seen video 1 yet but I like your color coding of the different objects, properties etc.

    I work with designing traffic signs on a 4 km long part of a new highway with 8 different scenarios depending on operators choice or accidents. All in all I have around 90 sheets in a workbook, each with a series of signals that introduces new signals down the road in the next time frame. I have up to 12 time frames but most scenarios only use 4-6. I want to learn how I can simulate each scenario to see if they work before I implement them in a PLC at site.

    I want to build a graphic over the highway in a separate sheet (plus exits and ramps, bypasses and all signs and text messages to drivers) with bottoms for each scenario (=a macro) showing what will happen over time. – and I want them to work on Office 2016 on both PC’s and Macbook Pro’s.

    Giant task to build such a macro system, but small compared to have the overview of “jumping around” from scenario to scenario without ANY fault presented before an imaginary driver.

    I know it can be done, I’ve seen it in another company with the same amount of data and complexity, – but I don’t know how to program it myself (yet).

    • Hi Marie,

      Wow, that sounds like a cool job and project. I would love to see that when you are finished with it. 🙂

      Just about anything is possible with VBA. The biggest challenge you might face is the 2016 MAC version. Unfortunately, there isn’t a real VB Editor in the new Mac version and it is almost impossible to program and debug. The 2011 version for Mac has a much more robust editor. I have done a few projects for the Mac version, including my Tab Hound Add-in, and it can be a bit of a challenge.

      On another note, I do have an Add-in called Tab Hound that might help you navigate the workbook with 90 sheets in it. It has a search feature that allows you to quickly search and navigate to any sheet in the workbook.

      Let me know if you have any questions. Thanks!

  • Interested in the use of the Index function and also help in clearing up the use of Offset and the n select the Offset range for action.

  • Thanks Jon – I’ve dabbled with the Macro recorder in the past, but recently I’ve been tasked with more Excel work, including working on one or more dashboards for the department here. Video one has given me a base to launch from and look at the ways in which Macros and VBA could help me with the background pivot tables etc. Thank you, and I am looking forward to the next videos.

    Tony

    • Thank you Tony! VBA can really help automate pivot tables and save us a lot of time. You can access just about every aspect of pivot tables through the VBA object model.

  • Hi Jon,

    Great first lesson in Marco and VBA, I am looking forward to the next sessions. I have 2 tasks that in the main I would like to speed up with the use of Marcos and VBA.

    The first one is to hide all the rows on a given worksheet, secure the worksheet with a regular password and then hide the worksheet. The aim of this is to secure all the data and workings that are in the background of the workbook before distributing. Ideally if the Macro could do this based on a tab color, or a particular reference in a set cell on each tab.

    The second is to copy the data from a worksheet from a named file and paste in to several different workbooks.

    Thank you
    Michael

    • Hi Michael,
      Those are great tasks for macros. Everything is possible with a macro including the code running based on tab (sheet) color. The tab color is a property of the worksheet object.

      Worksheets("Sheet1").Tab.Color
      Worksheets("Sheet1").Tab.ColorIndex

      Those lines of code will return the color (can be converted to RGB value) or the color index number on the color palette.

      I hope that helps get you started. Thanks again!

  • I really enjoyed the first video. I have used the recorder, but want to write my own macros. I have worksheets provided to me by other departments that group requirement numbers into one cell for a test case. I prefer to have each requirement covered in a separate cell (row). I would love to learn enough to be able to duplicate the information in the other cells in the row that lists the test case and then only put one of the multitude of requirement numbers currently in the requirement cell in the new row. The macro would need to run in each row the number of times that is equal to the number of requirement numbers in the cell.

  • Hi Jon – every week I download an excel report from our scheduling system that I then have to cleanse ( remove blank columns,pictures, insert another column and then copy a date to each row in the inserted column), and then repeat this for each day in the report. The end result is then copied into another excel table and then a pivot table based upon the excel table is then refreshed. I am looking to automate this process.

    Thanks

  • Thank you Jon. The first video was very educational, so simple but so profound (for me). I usually record a macro and adjust the code because I have never learnt how to write from scratch. Now that you have decoded the symbols and the qualifications for me, it will be a LOT easier. I don’t have a specific need right now – just always wanted to know more so thought I would finally make time. It was worth it.

    • Thank you Mary-Ann. I’m so happy to hear you learned from that video and will be writing your own macros. You are about to explore a whole new world of VBA. 🙂

  • I hope you’re well I saw your which was simpler than I found in YouTube.

    It gave me a clear understanding about what is the property and method.

    Thanks I hope I will find other videos as the same

    Regards

    Muhammad Yasin