8.11: Recording a Macro
- Page ID
- 46548
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Learning Objectives
- Use a macro to add a filter to column headers
- Use a macro to remove duplicates
- Use a macro to select blank rows
In business, you may have spreadsheets filled with data from different departments, divisions or even other companies requiring updating, reformatting or cleaning up on a regular basis. There are a few macros examples that help make this repetitive work easier.
These next spreadsheet actions are created from the View > Macros > Record macro path.
Adding Filters to Column Headers
Each quarter you receive a spreadsheet filled with information. To make it easier to sort data you’d like to add filters to each new spreadsheet. Here are the steps to follow to make a macro for this:
- Open worksheet > View > Macros > Record Macro
- In Record Macro dialogue box;
- Type in macro name with no spaces or by using _ to connect words (e.g. Filter_Macro).
- Create a shortcut key (optional). If you do choose one, make sure it isn’t Ctrl + C, Ctrl + V, or other already existing shortcut keys.
- Decide where to store the macro recording.
- Add a description (optional) to help identify the functions done by the macro and click OK. The macro is now recording.
- Highlight the entire row of column headers > Data tab > Filter button.
- Go back to View > Macros > Stop Recording. This macro is now available to use on the next set of data needing filters added.
- Run macro: To run the macro on the new set of data open the data, go to View > Macros > View Macros. A dialog box will open then select the macro for adding filters and click Run.
Watch this short video to see these tasks done in sequence and used on a fresh data set.
Remove Duplicates
Spreadsheet data is often filled with duplicate information and needs to be sorted through in order to be useful. For this example, a single list of the salespeople is needed from the large data set. To start recording the macro, follow the order in the example for recording above, then follow these steps:
- Highlight the column containing the Salesperson names. Right click > copy OR use Ctrl + C
- Open a new tab > select a cell > right click and paste OR Ctrl + V
- Click Data tab > Remove Duplicates button
- Go back to View > Macros > Stop Recording. This macro is now available to use on the next set of data needing duplicates reduced.
- Run macro: To run the macro on the new set of data open the data then go to View > Macros > View Macros. A dialog box will open, select the macro for removing duplicates and click Run.
Watch the short video to see these tasks done sequence and used on a fresh data set.
Selecting Blank Rows
Many times, a worksheet or data set is filled with blank rows that can stop a successful analysis of the data or cause errors in formulas. In order to clean up a data set with a macro, follow these steps.
Like with the other functions, open the spreadsheet and set up a macro for recording. An alternative way to quickly record is to select the record button at the bottom left corner of the Excel window to start.
- Create a new column > label it Empty (or another name). Make sure it has a filter added in the header.
- In the first cell, type in the function that counts how many values are in the list of arguments, that function is = COUNTA(B4:G4) your table range and press the Enter key.
- With the H4 cell highlighted, pull the corner down the entire column covering all the rows.
- Click filter arrow in “Empty” header and deselect “All” the select “0” and press OK.
- Select all the rows that are “0” > right click delete
- Click filter arrow in “Empty” header, then select Clear filter from “Empty” and data rows appear with no empty rows.
- Go back to View, Macros and Stop Recording. Alternatively, click the little square in the bottom left corner to stop the recording. This macro is now available to use on the next set of data needing duplicates reduced.
- Run macro: To run the macro on the new set of data open the data, go to View > Macros > View Macros. A dialog box will open, select the macro for selecting blank rows and click Run.
Watch the short video to see these tasks done in sequence and used on a fresh data set.
Practice Questions
Note
Unlike other actions, running a macro cannot be reversed with the Ctrl + Z (Cmnd+Z) short-cut or the Undo arrow so make sure you have a copy of the original data worksheet before running a macro for the first time.
Macros take a little practice to get used to, but once you use them more frequently, you will see the variety of things that a macro can accomplish. Remember, a macro allows you to record operations and re-use the sequence of mouse actions or keystrokes of anything you can do in Excel with keystrokes or a mouse. Start using them and see how far you can go.
Contributors and Attributions
- Recording a Macro. Authored by: Sherri Pendleton. Provided by: Lumen Learning. License: CC BY: Attribution