Mastering Excel Macros: Arrays
Format: PDF / Kindle (mobi) / ePub
This lesson covers the VBA topics: Arrays, Collections and Dictionaries. Each one of these items help you use macros to manage, calculate and process large amounts of information. Why are these needed? Because performing calculations in macro is exponential faster than performing calculations in a worksheet. Seriously faster. This lesson has a sample workbook where you can test and see the performance improvements.
If you are new to my lessons (by lesson 10 you shouldn't be...but who knows!), you will see a link in the lesson where you can download follow along workbooks so you can practice what you read.
I also don't abandon you after you buy the lesson, if you have any questions, you can email me and I will help you out.
Data. The data has 50 students. You are going to create a 50-item static array to process the data. Data Types I’ve covered data types before, but let’s have a quick refresher. Specifying the exact type of data that the array will store will make the array more efficient. Excel will store just enough memory to handle the data. You can use a ‘bigger’ data type that is needed and it won’t break anything. Honestly, you might never see any performance improvements depending on how complex your.
New values. After the loop is finished, you will put the result into cell F2. It will make much more sense when you see the example. Click the red dot in the breakpoint line to remove the breakpoint. Add lines to calculate the average and put the result in cell F2. When you run the macro, the array will be populated; the average will then be calculated and put in cell F2. Populate by Range Now, I’m going to show you another way to load data into an array where you don’t have to process or.
Two-dimensional array, you need to specify how many rows and how many columns in this format (1 to 10, 1 to 2). The next image will show you how to write the declaration statement. Declare a dynamic array of Variant data type called arrScores. You can use the shortcut range method to populate the array. Populate the array with this line: arrScores = Range("B2:C10").Value. Now you are going to write two nested loops to print out the results to the Immediate window. Why the Immediate window?.
Types! Add a new item that is numeric. Add this line: Students.Add 42. Change the Print line to display the second item. Run the macro to see the second item. Wait. Maybe you messed up and in this case, order matters. You need to add a new student before Susie. In an array, you would be out of luck. Thankfully, you are working with a collection. In a collection, it is easy to add an items in a specific spot in the collection. Add this line to add item James Polk before Susie Tink:.
Working on a very large, complex workbook that takes 10 or more minutes to calculate. Now the convenience of doing the calculation in a macro versus waiting for 10 minutes becomes apparent. Types of Arrays Arrays can be static or dynamic. Static arrays are fixed in size; you know exactly how many items you are going to store. Dynamic arrays are variable and can be resized as needed. Array Dimensionality Arrays have the concept of a dimension. You can think of a dimension as a field. A.