gyetvaig

VBA Get RGB of a cell background color

Jan 16th, 2019
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' ----------------------------------------------------------------
  2. ' Procedure Name: f_getRGBCellBackground
  3. ' Purpose: get the RGB value of a cell background color
  4. ' ----------------------------------------------------------------
  5. Function f_getRGBCellBackground(cell As Range, Optional partialColor As String) As Variant
  6.  
  7.     Dim backGroundColor As Long
  8.     Dim red As Long
  9.     Dim green As Long
  10.     Dim blue As Long
  11.  
  12.     backGroundColor = cell.Interior.Color
  13.    
  14.     red = backGroundColor Mod 256
  15.     green = backGroundColor \ 256 Mod 256
  16.     blue = backGroundColor \ 65536 Mod 256
  17.    
  18.     If partialColor <> vbNullString Then
  19.    
  20.         Select Case partialColor
  21.            
  22.             Case "red"
  23.            
  24.                 f_getRGBCellBackground = CLng(red)
  25.            
  26.             Case "green"
  27.                
  28.                 f_getRGBCellBackground = CLng(green)
  29.            
  30.             Case "blue"
  31.                
  32.                 f_getRGBCellBackground = CLng(blue)
  33.            
  34.             Case Else
  35.                
  36.                 f_getRGBCellBackground = "error in parameter"
  37.        
  38.         End Select
  39.        
  40.     Else
  41.    
  42.         f_getRGBCellBackground = Right("000" & red, 3) & " | " & Right("000" & green, 3) & " | " & Right("000" & blue, 3)
  43.        
  44.     End If
  45.    
  46. End Function
Advertisement