The `QBColor` function is a holdover from the QuickBASIC days and is available in VBA (Visual Basic for Applications) for compatibility purposes. The `QBColor` function takes an integer argument and returns a Long representing an RGB color value that can be used to set color properties in VBA, such as for forms, controls, or graphical elements.
The function accepts integer values between 0 and 15, with each value corresponding to a specific color as follows:
Index: Color
0: Black
1: Blue
2: Green
3: Cyan
4: Red
5: Magenta
6: Yellow (Brown)
7: White
8: Gray
9: Light Blue
10: Light Green
11: Light Cyan
12: Light Red
13: Light Magenta
14: Light Yellow
15: Bright White
Here’s how to use `QBColor` in VBA:
- Open Excel, Access, or another Microsoft Office application that supports VBA.
- Press `Alt + F11` to open the VBA editor.
- Insert a new module or use an existing one.
- Write a VBA procedure or function that utilizes the `QBColor` function.
For example, to set the background color of a cell in Excel to light red using `QBColor`, you can do something like this:
Sub SetCellColor()
Dim myColor As Long
myColor = QBColor(12) '12 corresponds to light red
'Assume that you want to color the cell A1
With ThisWorkbook.Sheets("Sheet1").Range("A1").Interior
.Color = myColor
End With
End Sub
When you run this VBA macro, it will set the color of cell A1 in “Sheet1” to light red. Keep in mind that the colors from `QBColor` are quite limited and may not look the same on modern displays as they did on the older monitors at the time QuickBASIC was popular due to differences in color depth and display technology.
For more control over colors, you can use the `RGB` function in VBA, which allows you to specify exact amounts of red, green, and blue:
Sub SetCellColorRGB()
Dim myColor As Long
myColor = RGB(255, 0, 0) 'RGB value for red
'Assume that you want to color the cell A1
With ThisWorkbook.Sheets("Sheet1").Range("A1").Interior
.Color = myColor
End With
End Sub
The `RGB` function provides more flexibility, as it can create a wide range of colors by mixing red, green, and blue values, which range from 0 to 255.