Macros & VBA Training Series Part 2:
Automate Excel: How to Use Loops to Create a Summary Report
Double-click video to view in Full Screen HD.
Download the file used in this video:
Macros & VBA Training Series
How can we automate this process further?
Please leave a comment below with your answer, and any questions. Thanks!


Thanks for watching! I will be sending you an email with video #3 in just a few days.
If you received the link to this page from a friend, you can signup here to get notified about the rest of the series.
Please leave a comment below with any questions. Thanks again and have a great day!
Fabulous presentation, am fortunate to have been on your list. Thank you for the opportunity to learn more about Excel. Much appreciated!
I strongly suggest that your tutorial are awesome are very easy to understand, I was a little bit confused about the loop section of the VBA but this tutorial teach a lot about looping and now I am confident about looping.
Thanks for posting in this manner.
regards
Narendra
Thank you Narendra! I’m so happy to hear that. Loops are an incredibly useful and powerful tool for automating processes in Excel.
I think that for some it will be a bit to much information at once, but at least it’s clearly shown
This is one of the best video on macros I have watched. Step-by-step and easy to follow. I considered myself an Excel frequent user, but still learned tricks and tips (such as .Formula, &Sheet name, Ctrl+y, etc.)
Many thanks, Jon!
From the pure macro point of view, I believe this video hit the bull’s eye. Great for automating a repetitive process, such as copy & paste, and links.
Just some thoughts from the modeling point of view. Andrew may ask for pure “system data” on one sheet, rather than multiple “system reports” by employee on separate tabs/files. That way, may be it is easy to pivot the data, and put Slicers on the Summary tab for manager’s drilldown.
Thanks Lenny! I really appreciate that and am happy to hear you learned a lot.
You are completely right about getting the system data. It would be easy to summarize the data with pivot tables if we had the underlying data that made up the reports. I have another video series on pivot tables where Andy learns this process as well. Thanks again Lenny!
Thank You for the very useful, clearly explained lesson. I look forward to the next lesson.
Thanks Marek!
Thanks for explaining the loop in VBA. Very clear.
Thanks Jan!
I’d like to see a dynamic grand total added to the end of the column (maybe autofit the columns). Would also like to see the values formatted in Currency and the grand total Bolded/double underlined.
Great video. I like your explanations because they are clear and concise though I think it might have gone a little too fast for someone new to VBA (though this is a refresher for me). I like your explanation of the For Next statement, look forward to your use of the Select Case and For Each statements explanations. Always had trouble differentiating between the For Each and For Next loops.
Michael
Thanks Michael! Those are great suggestions for polishing up the report.
Great question about loops. We used a For Next loop to loop through a collection of numbers in this video. The For Each loop is used when we are looping through a collection of objects. The following code would loop through each worksheet object in the workbook.
Dim ws as Worksheet
For Each ws in Activeworkbook.Worksheets
Next ws
This loop would have also worked for the summary sheets macro. However, we would have had to declare another variable to hold a number and then add to that number within each iteration of the loop.
Dim ws as Worksheet
Dim lRow as Long
For Each ws in Activeworkbook.Worksheets
Range(“A3”).Offset(lRow).Value = ws.Name
lRow = lRow + 1
Next ws
Picking the one to use might also depend on what you are doing with the worksheets that you are looping. If you are modifying the properties of the worksheet then you might want to use the For Each with a variable like ws because you will get the intellisense for ws. Each time you type ws. you will get the dropdown that contains all the properties and methods for ws (the worksheet object). This is nice if your macro includes tasks like renaming sheets, changing tab color, visible/hidden, protection, etc.
I hope that helps. In my upcoming VBA Pro Course I have three videos that go into the different types of For loops in more detail.
Thanks again Michael!
I have learned a lot in the two videos. I would like to see notes or written script of the presentation so I could refer to t when I set out to write my first VBA.
On video two, I would like to see how to add the titles etc on the Summary sheet, print header and footers, and printer instructions such as setting the print area, and any scaling necessary to print all on one sheet.
As I think about it, I receive all the raw data to first do the reports that show in the workbook. I could use information on how to format data in the worksheet, add columns where necessary to do additional calculations and the export the reports from the prepared data to individual sheets like you started with.
Hope I am not getting too greedy! 🙂
Hi Tom,
Those are great suggestions. I’m planning to add another video that shows how to cleanup and finalize the formatting on the summary sheet. I’ll let you know when it’s available. Thanks again!
I love how you can also download the workbook. These are great examples.
Thanks!
So cool. I’ve never worked in VBA before so I’m going to have to go back and watch the specific code a couple more times, but it’s wonderful to see the capabilities.
That’s awesome Danielle! I was hooked pretty quick after I first saw the power and speed of VBA. Writing your first macro can be a lot of fun. Thanks!
I’m getting an error after copying the hyperlink code. The error is “syntax error”. Why is this? Thanks much.
Hi Keisha,
Are you just copying that line into your own macro? If so, your macro would also have to contain the lSht variable. I’d be happy to take a look at your file. You can email it to me at [email protected].
Thanks!
Hi Jon,
Thanks for sharing your knowledge, the video is very well done and easy to follow.
I like the hyperlink technique, quite useful.
As suggestion, you could have mention some of the resources available that explain all the types of variables, since you introduce them here.
Thanks again,
Pablo
I really enjoyed your videos. Both were very informative and I am looking forward to the next ones. Question: What if each of the sales persons has a different number of rows and the total is on a different row – how would you bring the totals over to the summary sheet then. I have a project that has approx. 100 sheets and each sheet will have a different number of entries on it.
Thanks,
Came here to leave the same question. Really enjoying the videos, and this one has excellent explanation of the loop and variables, but I also have data where I need to find the last or a particular row of a report rather than a static D5 across the sheets. Please could this be addressed?
Thanks Andy! Please see my response to Carole on this issue. It’s a really great question.
Great question Carole! There are a few different ways to do that. I have an article that explains different ways to find the last used cell in a worksheet. One of these approaches will work if your total is in the last row of the spreadsheet. If not, you will need to determine some other logic for the total row. For example, maybe that row contains the word “Total” in column A. You can use the Range.Find method to find that value, then offset a certain number of columns to locate the cell you want to reference in the summary sheet. Check it out and let me know if you have any questions. Thanks!
Thanks for these videos, they are really easy to follow. This particular example is going to make my life so much easier! I frequently export lists from my POS to Excel so that I may enter them into a web interface; and I have been developing ways of automating this. This tutorial gave me the tools to cut 20 (!) minutes out of a task I do at least 3 times a week. Thanks for making this so easy!
Thanks Josh! That’s AWESOME! I’m so excited to hear that you can cut some time out of your week. 🙂 Thanks again and have a great day!
Thanks Jon – very informative and easy to follow.
Is it possible to skip sheets? i.e. I don’t want to include the “Summary” tab in the list every time I refresh the macro
Thanks
Graeme
Hi Graeme,
Great question. Yes, that is absolutely possible. One way to do it is to use an IF statement in the loop to check for the summary sheet. In the code below I added the IF statement to the macro. This will test if the current sheet in the loop is NOT named Summary. The less than and greater than symbols together (<>) is the logical operators for NOT. Please let me know if you have any questions. Thanks!
For lSht = 1 To lCount IF Worksheets(lSht).Name <> "Summary" Then Range("A3").Offset(lSht, 0).Value = Worksheets(lSht).Name Range("A3").Offset(lSht, 1).Formula = "='" & Worksheets(lSht).Name & "'!D10" ActiveSheet.Hyperlinks.Add Anchor:=Range("A3").Offset(lSht, 0), _ Address:="", _ SubAddress:="'" & Worksheets(lSht).Name & "'!A3" End IF Next lShtOk – solved it…
changed the “lSht = 1 to lCount” to read “lSht = 3 to lCount”
Cheers
Graeme
I didn’t see this comment before replying to your previous comment. Yes, that’s a great way to solve it too. As you can see there are a lot of different ways to do things with VBA. Makes it fun and challenging…
Great tutorial. Thank you much!
Loving the videos very clear explanations. Started wondering have you started into Office 365 API’s to integrate the different office products together?
Thanks Sayth! I haven’t done much with the Office API’s yet, as they are fairly limited in functionality right now. However, I think that will change in the future.
I’m going to have to watch the videos a couple of more times as a newbie…I’m trying to remember where the macro is named so that maybe I could add an update button to the top of the workbook so that if I had someone else entering data for me, they could hit the update button. We get workbooks with all 3 departments in them for each quarter. Each year is just added in so there are a lot of worksheets. It would be so nice to be able to skip to just the worksheets that are my department to pull the data. The names are consistent (e.g., SPED U14, SPED F13). Is there a way to do that? I’m also wondering if we named the data range we are pulling into the summary sheet, if each worksheet had a different number of entries, we’d still be able to pull the data in. Hope this makes sense. I’m enjoying a new world being opened up to me and looking forward to playing with the workbooks, watching the videos again, and applying the info to my work. Thanks, Jon!
Hi Cheryl,
Thanks for your comment! I’m happy to hear you are learning VBA. You can add a button or shape to the worksheet, then right click it and select Assign Macro… That will give you a list of your macros to choose from. Once the macro is assigned, click the button or shape will run the macro.
In regards to your question, I just posted some code to a similar question from Graeme below. You could test the worksheet names as you are looping through them. In your case you might want to just test that the first 4 letters of the sheet name start with SPED.
You could change that IF statement to something like the following.
IF Left(Worksheets(lSht).Name,4) = "SPED" ThenThis uses the Left function in VBA to return the first four character of the sheet name. If it is SPED, then the code will run to list the name on the sheet. If not, it will skip that code and go to the End IF line, then continue to the next sheet in the loop.
I hope that helps. Thanks again!
Thanks, Jon! I’m rewatching the videos and playing around with it now! Thanks for sharing your skill and knowledge!
Well done Jon!,
The contents of the course is really helpful and informative. Keep it up
Thank you Marcellin!
Hi Jon,
another great lesson, congrats!
Just like Carol and Andy, I also think it would be really useful to do the same with sheets where data are not on the same line. Including my company gives me the report of the agents I follow like this, because not all of them sell our entire product range, and so who only sells few items of us will have the total value in an upper position while the total value of who sells our full range will be located in a lower position. I noticed you shared some links on that already and I will go through it.
Additionally, I have a question concerning the hyperlink function: you said that you have previously recorded a macro to get that code, and then modified it for this case. Does it mean that the one you used it is like the standard code for hyperlinks functions, which then it has been further customized for this lesson?
Look forward to your reply
Best,
Hi Denis,
Great question. For the hyperlink I used the Insert Hyperlinks menu. You can get to it by right-clicking a cell and selecting Hyperlink… (Ctrl+K on the keyboard). I basically recorded this action with the macro recorder to produce the code. The code that the macro recorder produced was static, meaning it referenced the sheet name and range I selected while recording. I then changed the code to reference the sheet number in the loop, and also added the Offset property. When I get some time I will record another video that explains both questions in more detail.
Please let me know if you have any questions. Thanks!
Great training.
I like VBA but the code get messy.
I think I could format the number and do the subtotals etc.
Thanks for this example.
I was kind of easy to get complex things done.
Hi Jon
I am using Excel for Mc 2011 – does VBA run on this version?
Thanks
Hi Peter,
Yes, VBA is included with the Mac 2011 version of Excel. The VB Editor looks a little different, but the majority of the functionality is there.
At this time, the 2016 version for Mac does NOT contain a full VB Editor. I would not upgrade to 2016 if you are looking to develop VBA code for the Mac version. Thanks!
Excellent job! Keep up your brilliant job.
Thanks Jaderson! 🙂
Great training video! Just curious if a summary report can be generated if the individual employee’s data is on a separate excel document – not on worksheets within the same document? Thanks.
Hi M,
Yes that can definitely be done with VBA. I have an entire module dedicated to this topic in my VBA Pro Course. There are a series of lessons that explain how to create an application that will allow you to do all kinds of processes on a set of files. You can even create new files based on a template with this application.
I will be sending more info on the course this week. Thanks again!
Hi Jon,
Fantastic video. Thank you!
Suppose the total on each worksheet was not in cell D10, rather, another row in column D. How might you go about writing code to return the total? (Additionally, suppose the summary data weren’t in column D on each sheet…how could you go about finding the total if it were presented dynamically on each sheet?)
Thanks,
Mark
Great question Mark. You can use the Range.Find method to find the row based on a condition. Maybe that row contains the word total.
Here is an article and series of videos I have on how to find the last row with VBA.
Thanks!
Really nice! This is a potential solution to a long-time problem that just occurred to me thanks to VBA and I am very much looking forward for it to come to fruition!
But, before then, what if Andy receives the spreadsheets via email, puts them into a folder and now wants to create this same summary sheet stand alone without having to combine all the sheets manually into one workbook? Bonus questions: 1) What if he wants to put all the sheets into one workbook via VBA? 2) Say the files are on a network drive along with the ‘Summary’ sheet. How could we go about having the salespeople send this information to the Summary sheet with the press of a button, instead of Andy having to do it himself?
Your answers might give me some clues into tweaking the following code, which gives me file names from a folder. Ultimately, I want to pull info from a number of specific cells from the identical files within the folder to create a somewhat detailed report.
Private Sub CommandButton1_Click()
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer
Application.ScreenUpdating = False
directory = “C:\test”
fileName = Dir(directory & “*.xl??”)
Do While fileName “”
i = i + 1
j = 2
Cells(i, 1) = fileName
Workbooks.Open (directory & fileName)
For Each sheet In Workbooks(fileName).Worksheets
Workbooks(“files-in-a-directory.xls”).Worksheets(1).Cells(i, j).Value = sheet.Name
j = j + 1
Next sheet
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
End Sub
This is all quite daunting for me; so I look forward to your reply.
Many thanks for your instructions!
Givenson
Hi Givenson,
That is a great question. I have actually built an application I call the File Manager that allows you to automate any process on a set of files. It could definitely be used for the scenario you are speaking of.
I explain how to create the File Manger application in module 11 of my VBA Pro Course. We start from scratch and build it out through a series of video lessons in the course. I will be sending you more info on the course this week. Thanks!
Jon
I liked video two! Looking forward to video three. I will definitely have to use VBA more in my working practices within Excel.
Awesome! Thanks David!
Many thanks for an easily followed lesson.
Great presentation. Had the same question as Carole, and thanks for the answer. Also, can you take multiple results from the same sheet?
Great tutorial thank you!
I would agree with some of the previous requests for instructions on formatting the summary worksheet and perhaps setting up print parameters for management’s consumption. I really appreciate that you are starting with simple scenarios, allowing us to envision many uses for VBA macros both in our work and outside activities.
It would also be great to know how to direct an inserted hyperlink to a value reference on the target worksheet (e.g. based on an index match).
Thanks again!
Thanks Jennifer! I’m glad you enjoyed the tutorials. Those are great suggestions for further automating this process, and also a great suggestion for linking to the cells that contain amounts. I’ll look into creating tutorials for that in the future. Thanks again!
I am trying to write a formula in VBA that has quotation marks in the formula. Is there any way around making VBA think the quotation marks are text and not code.
Example: =if(A2=”No Match”,0,B2/C2)
Great question Jeremy! You will want to use the Chr function in place of the quotation marks. Character code 34 represents the quotation mark. So here is what your formula might look like.
“=if(A2=” & Chr(34) & “No Match” & Chr(34) & “,0,B2/C2)”
Thanks!
Great 2 videos for VBA
Great video! Thank you for taking the time to demonstrate to all of us “newbies”!
I do have a question; we have several hidden sheets within a workbook. What kind of code would you write to exclude the hidden worksheets for your summary page?
Looking forward to your next video!
Thanks Cindy! That’s a great question.
You can use an If statement to check if the sheet is visible.
For lSht = 1 To lCount If Sheets(lSht).Visible = True Then 'Code to list sheets End If Next lShtThat code will add blank lines to your summary sheet for each hidden sheet though. Here is another way to loop through the sheets using the worksheets object in the For Next Loop.
Dim ws As Worksheet Dim lSht As Long lSht = 1 For Each ws In ThisWorkbook.Worksheets If ws.Visible = True Then 'code to list sheets lSht = lSht + 1 End If Next wsThe code above loops through worksheets collection. The If statement checks if the worksheet is visible. We then use the lSht variable and add 1 to it every time the sheet is visible. This will only list the sheets that are visible, and create any blank rows in the list.
I hope that helps. 🙂
learning a lot, changing guess this is right to how to do it right.
I watched the video and gain something, thank you for sharing your knowledge
Awesome! Thanks Abdallah!
Thanks for the wonderful video!!
I have a query on VBA coding, can we write a macro to pull out values/data from pdf file?
Many thanks again.
Video #2 was great. In my COM200 class I use a Summary sheet (with my students) to ‘combine’ 4 worksheets (Ex. Central, East, North, West). We actually have been starting with =, then clicking each individual worksheet’s tab, then Enter to get the formula onto the Summary worksheet in its appropriate cell. I realize this is the long ‘method’, but what is instructed in our textbook.
The idea of the Macro you created in your video presentation will certainly save time, and something new I can teach my students.
Right on! Thank you.
I’ve enjoyed videos 1 and 2 and can wait to see video 3. They are very easy to follow….you are an awesome teacher! Thanks for taking the time to create these videos and for sharing them with us!
Crystal
Thanks for this excellent training video.
I really appreciate how you share your knowledge with us.
Thanks Lazar! Glad to hear you are enjoying the videos.
I wanted to say the same thing as all of you guys.
Jonas classes and training videos are great and very helpful.
Appreciate your help Jon. Thank you very much !
Thank you Dragan! You made my day! 🙂
Excellent presentation! Love how clear and easy to follow your instructions are!
Thank you so much!
Thank you Roger! I’m really happy to hear you enjoyed the video. 🙂
Thanks Jon. Really nice video !!! I have a question. I would like to copy data from cell D1 till D30 from sheet2 into sheet1 cell A1:A30. The problem here is that in cell D1:D30 there are duplicate records also which I want to remove. So could you please help me how can I write code for this?
Thanks again, Jon. It’s a very good example of saving time in generating a summary report for just a few lines of vba codes. By the way, if I have to include a title for the summary sheet, is it right to insert the vba codes below the “Next lSht”? Or this vba code for summary “title” can be written above the “For” lines of codes?
Hi William,
Great question! Yes, you can add the code for the title outside of the For loop. That means you can add it above the For line or below the Next lSht line. If you were to include the line inside the loop then it would run multiple times, which is unnecessary because you only need to title the sheet once. Let me know if you have any questions. Thanks!
Thanks Jon. Really appreciated your very clear explanation to my query.
Thanks William!
Hi Jon
Thanks for the video’s 1&2 are very helpful.
Okay, now you made that look really easy. I can’t wait to try it.
Thanks Terry! It definitely takes practice, but gets easier with time (and patience). 🙂
On a recent assignment I was documenting a Pivot Table-based monthly report. One of the worksheets held the majority of Pivot Tables, and they numbered nearly 200. I was looking for a way to extract the metadata into a report of its own, including the pivot table name, cell location, and range. I also wanted to do the same for the formulas found throughout the workbook. I imagine there are other, similar needs.
Any suggestions?
Hi Keith,
Wow that is a lot of pivot tables on one sheet! 🙂 For those types of tasks you can use a For Next Loop to loop through all the objects in the collection. For pivot tables you can loop through all the pivot tables on the sheet. Here is some sample code
Dim pt as PivotTableDim sName as String
For each pt in Activesheet.PivotTables
sName = pt.Name
'Add code for meta data
Next pt
The above code will loop through all the pivot tables on the active sheet. You will then need to loop through the pivot fields within each pivot table to get some of the other meta data. Here is an article that explains more about pivot fields and pivot items.
There is definitely a lot to learn with pivot tables and VBA. The good news is that most of the properties of a pivot table are supported in the object model, making it possible to automate a lot of common pivot table tasks.
I hope that helps get you started. Thanks!
I forgot to mention that these presentations are very useful and easy to follow. Good job.
Thanks Keith!
Great video tutorials…gets me motivated to seriously learn VBA!
Thanks Dennis! I’m happy to hear you are learning VBA.
I think your videos are awesome! I know management likes pictures. Can you add another video to show how to make this info into pretty graphics using other functions in VBA so that it updates when the summary sheet does and gets attention more readily? Right now, they have to look through the list and find the largest sales and it is not apparent why that sales is higher? Maybe more info on the basic sheets to slice or analyze???
Thank you Sue! I have another video series on Pivot Table and Dashboards that you might enjoy. Andy makes an appearance in that series as well, and he learns how to use pivot tables to quickly summarize data and create charts to make his findings look pretty. 🙂 I hope that helps.