-
Notifications
You must be signed in to change notification settings - Fork 20
/
ColorMacros.bas
65 lines (41 loc) · 1.34 KB
/
ColorMacros.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Attribute VB_Name = "Module1"
Option Explicit
Public Sub SetCellBackgroundColorFromHex(ByVal inputRange As Range)
Dim vaRGB As Variant: vaRGB = HexToRGB(inputRange.Value)
Dim R As Byte: R = vaRGB(0)
Dim G As Byte: G = vaRGB(1)
Dim B As Byte: B = vaRGB(2)
inputRange.Interior.Color = RGB(R, G, B)
End Sub
Public Sub SetCellFontColorFromHex(ByVal inputRange As Range)
Dim vaRGB As Variant: vaRGB = HexToRGB(inputRange.Value)
Dim R As Byte: R = vaRGB(0)
Dim G As Byte: G = vaRGB(1)
Dim B As Byte: B = vaRGB(2)
'this is a comment
inputRange.Font.Color = RGB(R, G, B)
End Sub
Public Sub SetSelectionColorFromHexInCell()
Dim element As Variant
If IsArray(Selection) Then
For Each element In Selection
SetCellBackgroundColorFromHex element
SetCellFontColorFromHex element
Next element
Else
SetCellBackgroundColorFromHex Selection
End If
End Sub
Public Function RGBToHex(R As Byte, G As Byte, B As Byte) As String
Dim output As String
output = Format(Hex(R), "00") & _
Format(Hex(G), "00") & _
Format(Hex(B), "00")
RGBToHex = output
End Function
Public Function HexToRGB(inputHex As String) As Variant
Dim R As Byte: R = Val("&H" & Mid(inputHex, 1, 2) & "&")
Dim G As Byte: G = Val("&H" & Mid(inputHex, 3, 2) & "&")
Dim B As Byte: B = Val("&H" & Mid(inputHex, 5, 2) & "&")
HexToRGB = Array(R, G, B)
End Function