Microsoft Office Tutorials and References
In Depth Information
Selecting a Color in a UserForm
Selecting a Color in a UserForm
The example in this section is a function that displays a dialog box (similar in concept to the
MyMsgBox function, presented earlier). The function, named GetAColor , returns a color value:
Public ColorValue As Variant
Function GetAColor() As Variant
GetAColor = ColorValue
End Function
You can use the GetAColor function with a statement like the following:
UserColor = GetAColor()
Executing this statement displays the UserForm. The user selects a color and clicks OK. The
function then assigns the user’s selected color value to the UserColor variable.
The UserForm, shown in Figure 15-23, contains three ScrollBar controls — one for each of the
color components (red, green, and blue). The value range for each ScrollBar is from 0 to 255. The
module contains procedures for the ScrollBar Change events. For example, here’s the
procedure that’s executed when the first ScrollBar is changed:
Private Sub ScrollBarRed_Change()
LabelRed.BackColor = RGB(ScrollBarRed.Value, 0, 0)
Call UpdateColor
End Sub
The UpdateColor procedure adjusts the color sample displayed, and also updates the RGB values.
Figure 15-23: This dialog box lets the user select a color by specifying the red, green, and blue components.
This example, named getacolor function.xlsm , is available on the companion
Search JabSto ::

Custom Search