12.3 Running Macros: Button, Shortcut and the Macros Dialog
Zoya just joined a small delivery team. Her shift handoff sheet needs two predictable actions: make the headings bold and place a checked total below three entered amounts. The Macros dialog lets her test each action directly. After both pass, she can give teammates a labeled control to run the procedure without looking through VBA code.
Connect the control to a specific action
Define what each command will change
QuickCart Delivery is fictional. The example resembles a team using a Blinkit-style delivery report, but the names, amounts and spreadsheet workflow are invented for this lesson. The morning report has three rows. Someone checks their money amounts against the order records. Someone else applies a style, then totals the verified amounts. Neither action needs to contact an external service. Keeping the two commands separate lets the reviewer test a simple visual change independently of a numerical one.
- Trigger: A trigger is what starts a procedure. In this lesson it may be a deliberate choice in the Macros dialog, a worksheet button, a shortcut or a toolbar control. Use a visible label to tell the next person what will happen before they click it.
- Procedure: It is a named sequence of VBA instructions. A public Sub in a standard module with no arguments is the usual entry point for an Excel macro you want to select from a dialog or assign to a simple button. A Sub performs actions and is introduced in a dedicated lesson.
- Assignment: Connecting a button to a procedure tells Excel which instructions to call. The label on the button can differ from the procedure's name. Changing the displayed text alone does not update the assignment.
- Result: For FormatDailyReport the effect belongs to the font of A1:D1 on the Report sheet. For WriteCheckedTotal the effect belongs to the value in D6. Keep D2:D4, the entries it checks, intact for both actions. Measure the intended outcome and one nearby cell that must survive.
Keep VBA inside the practice copy
Create a new training file in desktop Excel and save it as DailyRunPractice.xlsm. Keep your normal macro policy as it is. Open the code project's standard module from Developer Visual Basic. Some app configurations use different language or labels for the menu. The Macros dialog is a useful alternative to a keyboard shortcut. When your environment blocks VBA or you have only Excel for the web, read the code and work through the expected state changes without running it. Button objects can still be visible in a file even when the environment does not support their VBA action.
When a procedure uses ThisWorkbook it refers to its code's workbook. For these examples the code must be in DailyRunPractice.xlsm. Moving it to a personal macro workbook or an add-in would change which workbook ThisWorkbook names. This is one reason to start by testing the procedure directly. You can separately confirm that a button now points to that correct copy and the specific name in the picker.
Remember: A new way of starting the code does not change what the code does. Check the procedure in the Macros dialog before setting up a shape, control or key combination.
Choose a trigger you can explain and test
Begin in the Macros dialog
On the Developer tab, choose Macros. In Windows desktop Excel, Alt plus F8 is a common way to open that dialog. The Developer menu works when the keyboard sends that combination to another app. Check the Macros in list for workbook scope. Filtering to This Workbook when the practice file is active removes unrelated entries. Select the intended procedure and review its description, then choose Run. These are separate decisions when you have more than one workbook open.
A public entry point without input arguments is convenient because the dialog can call it directly. A helper routine may be private and take inputs supplied by other code, so it need not be offered to a person. The routine taking a worksheet object, for example, should get a checked worksheet from its caller. That helps keep the list from becoming a menu of low-level operations with unclear context. If your training procedure is missing, check its location, declaration and workbook before editing anything else.
Use a worksheet control when the actions belong to a report
- Form Control button: Add the button under Developer controls using the Form Controls choice where your desktop version presents it. Draw the button in an empty area, assign a named procedure and set clear label text. This is a straightforward desktop option for a worksheet command.
- Shape: Insert a simple rectangle or rounded rectangle. Label it clearly and choose Assign Macro from its context menu. A shape click can run an assigned macro. Check how the shape behaves in keyboard and mouse use on the supported app, and retain the Macros dialog as another way to run the command.
- ActiveX control: This is a different control technology with different platform and policy requirements. It is not needed for this lab. Use the ordinary Form Control route if you want a worksheet button. Only investigate another type when the workbook has a documented requirement and the environment supports it.
- Quick Access Toolbar: The actual route depends on the desktop client. In supported Windows versions you can choose File Options, then Quick Access Toolbar, select Macros as the command category and add the one you tested. This puts a shortcut in the app interface. A coworker who receives the workbook should not assume their toolbar will be set up the same way.
Add keyboard invocation only after checking for conflicts
Choose Options for the procedure in the Macros dialog. Microsoft documents lowercase combinations for Windows as Ctrl plus the letter and uppercase ones as Ctrl plus Shift plus the letter. Microsoft documents Option plus Command plus the letter for lowercase on Mac, with Ctrl as an additional option, and Ctrl plus Shift plus the letter for uppercase. Test the exact combination only in your own copy. Existing shortcuts can be overridden while the workbook with that macro is open. Consider whether keeping a known dialog or a visible button is easier for a new operator.
Suppose you assign a common editing key, then somebody opens the file during their normal shift. Pressing the familiar key may now call a procedure with a different effect. Using an uppercase key gives more options but does not guarantee the combination is free. Check the Excel shortcuts that your team relies on, OS bindings and other currently open macro workbooks. You can leave the field empty in your main practice file and record a proposed shortcut in the lesson notes instead.
Prepare three orders and one check total
Give each outcome a separate test cell
| Order ID | City | Status | Amount |
|---|---|---|---|
| QA-201 | Pune | Delivered | 125 |
| QA-202 | Nashik | Delivered | 250 |
| QA-203 | Pune | Pending | 164 |
Use Report as the worksheet name. Enter three numeric amounts, 125, 250 and 164. Put the label Checked total in A6 and leave D6 blank for the write. Add an independent worksheet formula in G2 to sum the current inputs. Keep this little formula separate from the number that the macro writes. After the valid run, both should show five hundred thirty-nine. Enter “Check this note” in G4, and keep a different sentence on a second tab called Notes.
G2: =SUM(D2:D4)
The two commands have simple names. If you already recorded a routine with one of these names, use a fresh practice workbook so the editor can distinguish all procedures. Insert a standard module in the project under DailyRunPractice and add the code below. The module is a place for storing procedures and declarations. It belongs to this workbook, so the destination of ThisWorkbook is explicit. If you later change Report's tab name you will need to update the way the code finds it.
Option Explicit
Public Sub FormatDailyReport()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Report")
ws.Range("A1:D1").Font.Bold = True
End Sub
Public Sub WriteCheckedTotal()
Dim ws As Worksheet
Dim amounts As Range
Set ws = ThisWorkbook.Worksheets("Report")
Set amounts = ws.Range("D2:D4")
If Application.WorksheetFunction.Count(amounts) <> 3 Then
MsgBox "Enter three numeric amounts in Report D2:D4."
Exit Sub
End If
ws.Range("D6").Value2 = Application.WorksheetFunction.Sum(amounts)
End Sub
- One property write: FormatDailyReport sets Bold for A1:D1. This makes a visible style last after the run, independently of how the procedure was triggered. It does not clear old data or add order rows.
- One group of inputs: WriteCheckedTotal reads exactly D2:D4. Count tests for three numeric results. A cleared cell or an amount entered as plain text fails the count. Excel counts an actual numeric zero, so zero produces a valid sum for this specific exercise.
- One result write: The sum is stored in D6 when all three entries pass the first check. That value describes the checked state at the time of the macro. G2 is an independent live formula. If D6 is stale after an edit, compare with G2 and inspect whether the macro ran. The formula can ignore a missing amount and still give a plausible number, so add the three-numeric-input check to the verification.
- One known limitation: This is a fixed-size fixture. Checking that the three cells are numeric does not establish that the report contains no other orders, nor that the amounts were copied correctly. If you add a fourth order, redesign the input range before treating this result as a complete report total. You will learn structured and dynamic range boundaries in later lessons.
Lab one. Test directly and isolate a failed check
Prove the two code paths before assigning controls
- Save the practice workbook and check that the Report header is ordinary and D6 is blank. Leave the G2 formula in place. Record the expected G2 value, 539. Select Notes so you can test whether the code returns to the intended Report object without using the displayed sheet.
- Open Developer Macros and set the scope to the practice workbook. Select FormatDailyReport and run it. Return to Report. A1, B1, C1 and D1 should all be bold. D6 should still be blank. Note G4 and the separate sheet as controls, since the procedure acts on a named range and does not depend on the visible cursor.
- Return the header to an ordinary style, run WriteCheckedTotal from the dialog and inspect the cells. D6 should show 539 even with the header ordinary. G2 is still 539 and the amounts are unchanged. The source code places only the sum in D6, so clicking the same command twice cannot add a second total row.
- Change D4 from 164 to 184. G2 becomes 559 in an ordinary automatically recalculating workbook. D6 retains its last stored value, 539, before a new action. Run WriteCheckedTotal to update D6 to 559. Read both cells independently. Change the input back to 164 and rerun to bring both values to 539 again.
- Clear D4 and run WriteCheckedTotal in the practice file. Expect the message about entering three numeric amounts and no write to D6. That cell keeps 539, the historical total. G2 may still show 375 from the other two cells. The contrast indicates a failed current run, so do not treat either number as the accepted total of three complete entries. Restore D4 and verify a successful run.
Follow a complete trace: The current sheet and a window's focus can affect which macro name is selected. The actual process only passes when you verify the name of the code workbook, the intended input values and the three separately observable cells: D6, G2 and G4. A label that says “Total” cannot certify the data by itself.
Separate the macro result from the invocation
Confirm the trace
Three inputs 125, 250 and 164 make 539. Keep G2 live and D6 blank until after your first run.
Run the named command
Pick only WriteCheckedTotal in the practice workbook. Check that D6 becomes 539 and the header remains ordinary.
Assign the right action
Place a button or shape with a label that says Write checked total. Inspect its assignment and keep the format command separate.
Verify the equivalent effect
Clear D6 in the test copy, use the control and check the three source amounts and 539. The button must match the dialog's effect.
Lab two. Wire both actions and verify what actually runs
Place and name the controls
- In the disposable DailyRunPractice file, place a Form Control button in an empty area to the right of the table, away from the existing formula and note. When the Assign Macro dialog opens, choose the reviewed FormatDailyReport procedure from this file. Rename the label to Format report header. Right-click or use the context menu for object editing when your desktop version offers it.
- Insert a simple labeled shape under the button and call it Write checked total. Use its Assign Macro context action and choose WriteCheckedTotal from the same workbook. Keep the controls far enough apart that a user will not mistake one for the other. The workbook should still be readable without relying on the shapes as headings.
- Reset the header to ordinary and clear D6. The input values remain 125, 250 and 164. Use the Format report header control first. The four labels should become bold, D6 stays blank, the live G2 stays 539 and neither note changes. If the worksheet just got a number in D6, inspect the assignment instead of editing the known passing format procedure.
- Use the Write checked total control to fill D6. It should now equal G2. Confirm that the same result appears when you clear D6 and run the corresponding command in the Macros dialog. This proves both triggers led to the same procedure for this test.
- Save the training file, close it and reopen that exact XLSM copy. Verify both assignments before running either control. If the file was copied, renamed or opened beside an older copy, inspect whether a macro reference still points to the correct file. Keep the original practice copy until this round of testing is complete.
Recreate a wrong assignment without touching real data
Take a separate lab copy with a clear test name. Reassign the shape labeled Write checked total to FormatDailyReport, then start with D6 empty and the header ordinary. Activating the shape should leave D6 empty and make the header bold. This is intentionally wrong pairing. The distinctive effects make it possible to diagnose the problem before changing the procedure source. Keep the data and formulas available to prove that the failure came from the connection.
Use the Assign Macro picker to correct the shape. Verify it points to the copy's WriteCheckedTotal, then run it with the heading left ordinary and D6 cleared. You should see 539 only in D6 and notice that the header stayed ordinary. Restore or discard the separate exercise copy. If your client does not let you insert or assign a control, use a paper test with two cards labeled for the actions and lay each card next to the expected result. Then deliberately swap the labels and describe the symptom.
Try one optional entry point after the main test
On an approved desktop, use the Macro Options dialog to check whether the shortcut field is empty. If you choose to add a shortcut, use an isolated practice copy and only a key combination that you verified is unused in your setting. Record the platform and key. Click away from the code editor and repeat the clear-and-run test for the designated action. Check the same cells as you did before, remove the temporary shortcut and close the copy. A course screenshot of someone else's key setting cannot prove that your OS or client uses the same one.
You may prefer a local Quick Access Toolbar control because you open this report repeatedly. If the app supports assigning a macro to it, test directly first and add only the single needed command. Inspect which workbook owns the macro each time you switch files. Remove the training shortcut after using it to compare. A worksheet button generally travels with the saved practice workbook, while local toolbar configuration usually belongs to the Excel user interface. Either depends on having suitable permission and the intended code available.
Diagnose a failed control by observing one result
Ask which of three links failed
- The procedure is missing from the dialog. Check the active file and Macros in scope. See whether the project was saved as XLSM and whether the public zero-argument procedure is in a standard module. Compare a file with an open same-named older copy. If your client is a browser, its worksheet view alone cannot run the desktop VBA routine. Keep app, storage and security observations separate.
- The command runs from the dialog but the button fails. Inspect its assignment and confirm the procedure is actually in this workbook. In Excel desktop clicking a shape can run a named macro through its action property. Another file may once have supplied that procedure. Check whether the desired macro was renamed after the control was assigned. Reassign deliberately and repeat a fresh-start test.
- The button calls the right procedure but the result is missing. Try the same input in the Macros dialog. Check the two three-value inputs: amounts count and sum. If an amount is empty, the guarded routine issues a message and leaves D6 alone. If the result already matched, start with D6 cleared in the practice file so you can see whether it was written. Watch for an expected validation message before treating a stale result as today's number.
- Two people report different behavior with the same key. Record their versions and platforms, the open workbook list and the expected command. In many cases they can reproduce the behavior through the Macros dialog first. Decide whether a visible labeled control will be clearer than maintaining multiple customized shortcuts. Avoid changing common editing keys for general users.
Compare what is visible and what is stored
If the report is shared as a macro-free copy, it may still retain formatting and shapes even after eliminating the VBA project. A reader evaluating that copy should verify that any button intended to remain usable has the underlying procedure and appropriate host. The reporting owner can give the macro-free workbook an unmistakable name and use G2 for a live total. A separate internal XLSM training version can demonstrate the two commands. The user should know which file they are editing and what its controls can do.
In a future chapter the Report sheet may show three controls called Format Report, Split by City and Combine Sheets. Their actions would have different destinations and responsibilities. Merely labeling a shape Split by City does not create that logic. First define where the new sheets go, how a duplicate city behaves and what a rerun is allowed to replace. Use the practical macros lesson when those procedures are ready to test. Then give each macro a distinct verification step and a named copy for testing.
Choose one experiment: If you see 559 in G2, 539 in D6 and both Notes markers intact, decide whether that is a bad button assignment or a stale checked total first. Look for what changed in the inputs and test WriteCheckedTotal directly with three numbers. Do not assume a discrepancy proves an unrelated format procedure ran.
Write an acceptance sheet for the next shift
Use a specific before and after
An acceptance test is an observation that shows whether a requested behavior actually worked. The request “Make the report easy to use” is too wide to test directly. You can test “Sana can format the header using the control without changing the checked total, and she can rebuild the total using the other control.” Give her the known starting data and document which visible outputs she should see. A finance analyst who cannot run VBA can still check the arithmetic and see why the expected macro behavior makes sense.
| Test | Header | D6 | G2 |
|---|---|---|---|
| Format from plain header | Bold | Blank | 539 |
| Write from empty D6 | Ordinary | 539 | 539 |
| Write with D4 cleared after a successful run | Ordinary | 539 retained | 375 |
| Write with D4 at 184 | Ordinary | 559 | 559 |
The third row deliberately distinguishes the previous stored result from the current input. Record the message, the fact that D6 was not written and which order lacked an amount. The success result plus the failure result establish more than simply noticing that there is always a number in the total area. Reconnect the correct control and restore valid data before the workbook changes hands.
Mark manual and automatic steps honestly
- Reading the data: The report owner checks input records. Quantity and status may have come from an export, but this little command reads only the amount column. Record the exact workbook and cell area that the person checked. A total for three numbers cannot discover the missing fourth order on its own.
- Triggering the procedure: The person uses a plain-labeled control or the Macros dialog. Write the alternative so somebody who cannot click a shape can still use the command from a keyboard and menu in desktop Excel if their environment permits. Do not require them to guess which icon activates the report.
- Reading the results: The operator checks the count and amounts before trusting D6. Use a clearly written note beside the output to say it updates only when Write checked total runs successfully. A worksheet formula in a separate cell lets the operator see a change the moment the input recalculates, but its ability to ignore empties makes it insufficient for a completeness check.
- Handling a failure: Give the next person one location and one clear action. For a missing amount that means check D2 through D4, then run again. If a workbook cannot run macros under policy, use the workbook's worksheet formula for independent arithmetic and handle the completeness check manually.
Know what the worksheet check includes
Write checked total includes every numeric amount in the three-cell range, regardless of status. In the practice report there are two Delivered rows and one Pending row. Add the delivered figures by hand and you get three hundred seventy-five. Adding the pending amount produces five hundred thirty-nine. Both are useful answers to different business questions. A label such as Delivered total would promise the wrong input rule if you attached it to WriteCheckedTotal. It needs a procedure that reads the status along with each amount or a separate worksheet check using that status as a condition.
Sometimes a person hides the pending row, expecting the macro to see what is visible on screen. The fixed D2:D4 range still includes it. The worksheet function called here sums what it reads in that range. Test this on a fresh practice copy by hiding the third row and predicting that the output remains 539. Restore the row immediately after reading the result. Write down whether the real job needs visible rows, approved statuses or a fixed set of entries, since each calls for a distinct input rule.
Likewise, a numeric date serial is still a number and will pass the numeric count. That is useful for understanding the technology behind copying cells and a reason to check the source's meaning before using the fixture in a real delivery report. The amount column should contain amounts in one stated unit, not a mixture of dates and money that merely passes the type check.
Walk through two ownership changes
Suppose Zoya takes a vacation and Sana opens DailyRunPractice on another machine. The saved worksheet can have the assigned shape. The new machine may have no matching Quick Access Toolbar button. Start with the code workbook, choose the named macro in the dialog and test on a clean copy. If it works, compare the worksheet control. This separates an application configuration issue from a shared workbook fault. A little note on the Report tab with the menu path can save an operator from looking for an icon created on a different machine.
Next suppose the team renames both the file and one procedure as part of a handoff. In the copied file, inspect the assignment for the shape even if its label looks unchanged. Reassign the correct macro name in that file after reviewing the destination it writes. Run one successful and one intentionally incomplete row test. If you can reproduce both in the new file, put its proper filename on the handoff note. Keep the old file separate so nobody mistakenly opens it thinking it contains the new control wiring.
Now consider operating without buttons. You can list each step in a support note: select the practice workbook, open the Macros dialog, choose This Workbook, select WriteCheckedTotal, run and read D6. The number of decisions provides a reason to add a good control later. The steps themselves provide a recovery route if a shape is hidden or mistakenly assigned. Keep both paths available while somebody else verifies the new report.
Quick checks
Identify the broken link
The format button now fills D6. Which detail do you inspect first?
Inspect the actual macro assignment. FormatDailyReport should set Bold in A1:D1. Writing a total suggests the button points to WriteCheckedTotal. Run FormatDailyReport directly in a disposable copy to verify it still works. Fix the assignment if the direct run is correct. Check that the button's displayed label explains the new attachment.
The shortcut changes Undo while a workbook is open. How could that happen?
A macro shortcut can override an equivalent built-in Excel shortcut while the workbook containing the macro is open. If the assignment belongs to your practice file, remove it and use the dialog. For a real shared file describe the conflicting combination and let the maintenance owner restore a deliberate entry point for the user.
Why does D6 still show 539 after you change one source to 184?
The macro wrote 539 from the earlier data. D6 holds that stored number until a successful rerun. The independent formula in G2 rises to 559 after recalculation. If the action runs with all three numeric amounts, D6 should become 559 too. The new amount differs by exactly twenty, so you can check that without code.
Can the checked total use a text entry that looks like 164?
The specific procedure counts numeric cells in its input range. A textual 164 does not qualify as a number in that range, so the count is two and the action should exit before updating its result. Verify the actual cell value, correct the input type if appropriate and rerun. The report can otherwise retain an older total.
A teammate opens the XLSM file in a browser and sees both shapes. What can they do?
They can inspect the workbook data and formula check in the browser, where supported. Excel for the web cannot run or edit the VBA behind the shapes. Use approved desktop Excel for the lab's code path. Keep the exact file and procedure list available so somebody with that environment can verify the automation.
When does the main report receive a new total row in this lab?
The procedure writes only to D6, a reserved result cell. It does not create or append a row each time you run it. A repeated run with unchanged inputs should leave 539 in the same place. This makes duplicate trigger tests easy to interpret even when several controls call the same macro.
Interview and discussion
Use the fixture to explain how you would support a workbook
How do you tell an invocation issue from a logic error?
Run the procedure directly from the Macros dialog with the same valid input and an intentionally reset output. If it produces 539 there but the button makes the header bold, inspect the button's assignment. If both routes fail with the same three values, inspect how the code targets the workbook, validates the data and writes the result.
What kind of procedure makes a practical entry point for worksheet controls?
Use a public Sub in a standard module with no parameters for the worksheet action in this exercise. Give it a clear name and validate its context before passing details to helpers. This lets the dialog or assigned control call it without input arguments. A code helper can accept a worksheet or range that the entry point already checked.
How would you make a button easier to maintain after renaming a file?
Keep the macro with the intended workbook and give the button a label that describes the action. Recheck which workbook and macro the copied shape really invokes after a rename. Verify a fresh-start test and a known failure in the copy. Record the workbook type and an alternative path through the dialog for support.
What evidence do you record for a rejected input?
Record that one amount is blank, that the fixed input count must be three, and that the procedure reported the missing numeric amount. Record that D6 retains the result from a previous successful run and that G2 now sums only two inputs to 375. A rejected run can legitimately leave previously written values untouched.
Why keep the format command and the total command separate in this first report?
They have different scopes and success checks. Bold belongs to a header range while the result is one amount computed from three cells. Separate entry points let a reviewer confirm that styling cannot disguise a failed total. If later both actions are part of one approved workflow, test the separate actions before combining them in an explicit sequence.
Six recall cards
- Direct proof
Run the named macro once in the dialog with a known input before connecting any visual control or shortcut.
- Shape connection
Assign Macro names the procedure. Label text describes it to the reader. Verify each independently after editing or copying.
- Worksheet scope
The formatting demo targets Report A1:D1. The count and sum demo reads D2:D4 and writes one value to D6.
- Shortcut conflicts
Macro keys can override existing shortcuts while the file is open. A visible control and the Macros dialog remain clearer for many teams.
- Checked snapshot
Compare D6 with G2 and confirm three numeric inputs. A failed run does not automatically clear the old result.
- Reopen test
Keep the XLSM code workbook. Reopen it by name, check the assigned actions and confirm that policy and app support a run.
Microsoft references
Continue practising: explore the existing Excel Task Hub for additional exercises.