Use Controls from the Worksheet Class
A key advantage of the Forms 2.0 controls is that they can interact with the worksheet class. That’s an abstract advantage best illustrated by a short example:
Create a new worksheet.
Click the
SpinButtoncontrol on the Control Toolbox and draw the control at the edge of cell B2 on the worksheet, as shown in Figure 20-18.
Figure 20-18. Using a SpinButton to set the value of a cell
Click View Code on the Control Toolbox. Excel displays the sheet’s class in the Visual Basic Editor.
Enter the following code in the spin button’s
Changeevent procedure (shown in bold):Private Sub SpinButton1_Change( ) SpinButton1.BottomRightCell.Offset(-1, -1).Value = SpinButton1.Value End SubReturn to the worksheet and click Exit Design Mode on the Control Toolbox.
The cool thing about this sample is that if you move the SpinButton to another location, the effect of clicking up or down moves to the adjacent cell. That’s possible because the control is a member of the sheet’s class.
One thing you’ll notice about this sample is that the SpinButton stops spinning at 0. That’s because the control’s Min property is 0 by default. To change that:
Click Design Mode on the Control Toolbox.
Select the
SpinButtoncontrol on the worksheet.Click Properties on the Control Toolbox. Excel displays the Properties dialog box (Figure 20-19).
Change the
Minproperty to-100and click Exit Design ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access