Cell Color Index not Accurate and How to Convert the RGB String to a Color?

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any assistance provided. I am trying to use an existing cells Interior Color Index number to shade other cells, but the color it retrieves is different.
Range("C10") is the existing color. When I retrieve it and use it on Range("D10") , I get what is shown

Trading Videos - (Active).xlsm
CD
10
Al.Brooks (2)


Why is the color different and how can I get the actual color?
I tried to use a function where I can obtain the RGB color code to see if it will give me a more accurate color index, but how do I convert the string to a color?

So why am I getting an error on the following line:
VBA Code:
Range("D10").Interior.ColorIndex = Evaluate(ColorRGB_Is)

VBA Code:
Option Explicit

Public Sub ColorTest()

Dim Rng As Range
Dim ColorRGB_Get As String
Dim ColorRGB_Is As Variant
Dim ClrInt As Long

    Set Rng = Range("C10")

    'ColorRGB_GetF(Rng As Range) As String
     ColorRGB_Get = ColorRGB_GetF(Rng)
    
    ColorRGB_Is = "RGB(" & ColorRGB_Get & ")"
    
    Range("D10").Interior.ColorIndex = Evaluate(ColorRGB_Is)
    
End Sub


Function ColorRGB_GetF(Rng As Range) As String
  
 Dim IntColor As Long
 Dim RGB_Color As String
 Dim c As Long
 Dim r As Long
 Dim G As Long
 Dim b As Long
 
 IntColor = Rng.Interior.Color
 r = IntColor And 255
 G = IntColor \ 256 And 255
 b = IntColor \ 256 ^ 2 And 255
 ColorRGB_GetF = r & ", " & G & ", " & b
 
End Function
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this instead.

VBA Code:
Public Sub ColorTest()
    
    Dim Rng As Range
    Dim SA, R, G, B
    
    Set Rng = Range("C10")
    
    SA = Split(ColorRGB_GetF(Rng), ",")
    
    R = Val(SA(0))
    G = Val(SA(1))
    B = Val(SA(2))
    
    Range("D10").Interior.Color = RGB(R, G, B)
End Sub
 
Upvote 0
Solution
Try this instead.

VBA Code:
Public Sub ColorTest()
   
    Dim Rng As Range
    Dim SA, R, G, B
   
    Set Rng = Range("C10")
   
    SA = Split(ColorRGB_GetF(Rng), ",")
   
    R = Val(SA(0))
    G = Val(SA(1))
    B = Val(SA(2))
   
    Range("D10").Interior.Color = RGB(R, G, B)
End Sub
Thank you so much @rlv01! That worked perfect!
 
Upvote 0
Would this not be sufficient?
Code:
Cells(10, 4).Interior.Color = Cells(10, 3).Interior.Color
     'or
Range("D10").Interior.Color = Range("C10").Interior.Color
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,030
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top