VBA - Activity Day 6
VBA - Activity Day 6
In this lesson, you attach a macro to a Forms button that will toggle certain columns as being visible or
hidden. Along the way, you learn a few tricks about faster methods for entering data into multiple cells.
Lesson Requirements
For this lesson, you place a Forms button on a worksheet that contains a hypothetical table of monthly
income activity for a department store’s clothing items. A macro will be attached to the button that,
when clicked, will toggle columns or rows as being hidden or visible, depending on how you want to see
the data. Upon each click of the button, the cycle of views will be to see the entire table’s detail, see
totals only by clothing item, or see totals only by month. This lesson also includes tips on fast data entry
by using the fill handle and shortcut keys.
Step by Step
2. On your active worksheet, list the months of the year in range A6:A17. You can do this quickly by
entering January in cell A6, then selecting A6, and pointing your mouse over the fill handle, which is the
small black square in the lower-right corner of the selected cell. You know your mouse is hovering over
the fill handle when the cursor changes to a crosshair, as indicated in Figure 13-17. Press your left mouse
button onto the fill handle, and drag your mouse down to cell A17 as indicated in Figure 13-18. Release
the mouse button, and the 12 months of the year will be filled into range A6:A17 as shown in Figure 13-
19.
4. Enter sample numbers in range B6:F17. There is nothing special about the numbers; they are
just for demonstration purposes. To enter the numbers quickly as shown in Figure 13-20, do
the following:
= INT(RAND()*1000).
the range.
➤ Right-click somewhere in the range B6:F17, and select Paste Special ➪ Values ➪ OK.
6. Select the column A header, which will select all of column A. Right-click onto any cell in column A,
select Column Width, enter 20, and click OK.
7. Quickly enter Sum functions for all rows and columns. Select range B6:G18, as shown in Figure 13-21,
and either double-click the Sum function icon or press the Alt+= keys.
8. With range B6:G18 currently selected, right-click anywhere in the selection, select Format Cells, and
click the Number tab in the Format Cells dialog box. In the category pane select Currency, set Decimal
Places to 0, and click OK as indicated in Figure 13-22. Your result will resemble Figure 13-23, with
different numbers because they were produced with the RAND function, but all good enough for this
lesson.
9. The task at hand is to create a macro that will be attached to a Forms button. Each time you click the
button, the macro will toggle to the next of three different views of the table: seeing the entire table’s
detail, seeing totals only by clothing item, or seeing totals only by month. To get started, press Alt+F11
to go to the Visual Basic Editor.
11. In your new module, type Sub ToggleViews and press the Enter key. VBA will produce the following
two lines of code, with an empty row between them:
Sub ToggleViews()
End Sub
12. Because the macro will hide and unhide rows and columns, turn off ScreenUpdating to keep the
screen from flickering:
Application.ScreenUpdating = False
13. Open a With structure that uses Application.Caller to identify the Forms button that was clicked:
With ActiveSheet.Buttons(Application.Caller)
14. Toggle between views based on the button’s captions to determine which view is next in the cycle:
End With
16. Turn ScreenUpdating on again:
Application.ScreenUpdating = True
17. Your entire macro will look like this:
Sub ToggleViews()
‘Turn off ScreenUpdating.
Application.ScreenUpdating = False
‘Open a With structure that uses Application.Caller
‘to identify the Forms button that was clicked.
With ActiveSheet.Buttons(Application.Caller)
‘Toggle between views based on the button’s captions
‘to determine which view is next in the cycle.
If .Caption = “SHOW ALL” Then
With Range(“A5:G18”)
.EntireColumn.Hidden = False
.EntireRow.Hidden = False
End With
.Caption = “MONTH TOTALS”
ElseIf .Caption = “MONTH TOTALS” Then
Range(“B:F”).EntireColumn.Hidden = True
.Caption = “ITEM TOTALS”
ElseIf .Caption = “ITEM TOTALS” Then
Range(“B:F”).EntireColumn.Hidden = False
Rows(“6:17”).Hidden = True
.Caption = “SHOW ALL”
End If ‘for evaluating the button caption.
‘Close the With structure for Application.Caller.
End With
‘Turn ScreenUpdating on again.
Application.ScreenUpdating = True
End Sub
18. Press Alt+Q to return to the worksheet.
19. Draw a Forms button onto your worksheet at the top of column A. When you release the
mouse button you will see the Assign Macro dialog box. Select the macro named ToggleViews
21. Change the button’s caption to SHOW ALL as seen in Figure 13-26.
22. Select any cell to deselect the button. Click the button once and nothing will change on the sheet
because all the columns and rows are already visible. You will see that the button’s caption changed to
MONTH TOTALS. If you click the button again, you will see the month names listed in column A, and
their totals listed in column G. The button’s caption will read ITEM TOTALS. Click the button again and
you’ll see the clothing items named in row 5, and their totals listed in row 18. The button’s caption reads
SHOW ALL, and if you click the button again, all rows and columns will be shown.
23. You can continue cycling through the table’s views in this manner, by clicking the Forms button for
each view that you coded into the ToggleViews macro.
Activity #2
In this lesson you create an embedded pie chart, position it near the source data, and give each legend
key a unique color. The pie will have four slices that will each be given a unique color, and will each
display their respective data labels.
Step-by-Step
1. Insert a new worksheet and construct the simple table as shown in the
Figure.
4. In your new module, enter the name of this macro, which I am calling
TryItPieChart. Type Sub TryItPieChart, press the Enter key, and VBA will produce the following code:
Sub TryItPieChart()
End Sub
5. Declare the ChartObject variable.
chtQuarters.Chart.SetSourceData Source:=Range(“A3:B7”)
8. Define the type of chart, which is a pie:
chtQuarters.Chart.ChartType = xlPie
9. Activate the new chart to work with it:
ActiveSheet.ChartObjects(1).Activate
10. Color the legend entries to identify each pie piece:
With ActiveChart.Legend
.LegendEntries(1).LegendKey.Interior.Color = vbYellow
.LegendEntries(2).LegendKey.Interior.Color = vbCyan
.LegendEntries(3).LegendKey.Interior.Color = vbRed
.LegendEntries(4).LegendKey.Interior.Color = vbGreen
End With
11. Add data labels to see the numbers in the pie slices:
ActiveChart.SeriesCollection(1).ApplyDataLabels
12. Edit the chart title’s text.
ActiveChart.ChartTitle.Text = “Quarterly Sales”
13. Format the legend:
ActiveChart.Legend.Select
With Selection.Font
.Name = “Arial”
.FontStyle = “Bold”
.Size = 14
End With
14. Deselect the chart by selecting a cell:
Range(“A1”).Select
15. Press Alt+Q to return to the worksheet, and test your macro, which in its entirety will look as follows.
The result will look like the figure below, with a pie chart settled near the source data.
Sub TryItPieChart()
End Sub
Reference