Guest User

Untitled

a guest
May 24th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. ActiveCell.DisplayFormat.Interior.Color
  2.  
  3. ActiveCell.Interior.ColorIndex - one of 56 preset colors
  4.  
  5. ActiveCell.Interior.Color - RGB color, used like that:
  6.  
  7. ActiveCell.Interior.Color = RGB(255,255,255)
  8.  
  9. Range.DisplayFormat.Interior.ColorIndex
  10.  
  11. Range.Interior.ColorIndex
  12.  
  13. Public Function iColor(rng As Range, Optional formatType As String) As Variant
  14. 'formatType: Hex for #RRGGBB, RGB for (R, G, B) and IDX for VBA Color Index
  15. Dim colorVal As Variant
  16. colorVal = rng.DisplayFormat.Interior.Color
  17. Select Case UCase(formatType)
  18. Case "HEX"
  19. iColor = "#" & Hex(colorVal Mod 256) & Hex((colorVal 256) Mod 256) & Hex((colorVal 65536))
  20. Case "RGB"
  21. iColor = (colorVal Mod 256) & ", " & ((colorVal 256) Mod 256) & ", " & (colorVal 65536)
  22. Case "IDX"
  23. iColor = rng.Interior.ColorIndex
  24. Case Else
  25. iColor = colorVal
  26. End Select
  27. End Function
  28.  
  29. 'Example use of the iColor function to get the background color of selected cells
  30. Sub Get_Background_Color_Selection_Cells()
  31. Dim rng As Range
  32.  
  33. For Each rng In Selection.Cells
  34. myCell.Offset(0, 1).Value = iColor(rng, "HEX")
  35. myCell.Offset(0, 2).Value = iColor(rng, "RGB")
  36. Next
  37. End Sub
Add Comment
Please, Sign In to add comment