Пример VBA функции для генерации цвета

vba function example random hex color generate

Небольшой пример VBA кода который генерит рандомный цвет в виде HEX кода.  

Необходимо эти две функции добавить в вба модуль и потом в ячейке прописать  

=RandomColor() 

 

Function RandomColor() As String
  Bottom = 0
  Top = 15 
  r1 = DigitToHex(Application.WorksheetFunction.RandBetween(Bottom, Top))
  r2 = DigitToHex(Application.WorksheetFunction.RandBetween(Bottom, Top))
  g1 = DigitToHex(Application.WorksheetFunction.RandBetween(Bottom, Top))
  g2 = DigitToHex(Application.WorksheetFunction.RandBetween(Bottom, Top))
  b1 = DigitToHex(Application.WorksheetFunction.RandBetween(Bottom, Top))
  b2 = DigitToHex(Application.WorksheetFunction.RandBetween(Bottom, Top))
  RandomColor = "#" + r1 + r2 + g1 + g2 + b1 + b2
End Function
Function DigitToHex(val As Integer) As String
  Select Case val
     Case 1
        DigitToHex = "1"
     Case 2
        DigitToHex = "2"
     Case 3
        DigitToHex = "3"
     Case 4
        DigitToHex = "4"
     Case 5
        DigitToHex = "5"
     Case 6
        DigitToHex = "6"
     Case 7
        DigitToHex = "7"
     Case 8
        DigitToHex = "8"
     Case 9
        DigitToHex = "9"
     Case 10
        DigitToHex = "A"
     Case 11
        DigitToHex = "B"
     Case 12
        DigitToHex = "C"
     Case 13
        DigitToHex = "D"
     Case 14
        DigitToHex = "E"
     Case 15
        DigitToHex = "F"
     Case 0
        DigitToHex = "0"
   End Select
End Function