Closed Thread
Results 1 to 10 of 10

macro to show RGB numbers of a selected cell.

Hybrid View

  1. #1
    abhay_547
    Guest

    macro to show RGB numbers of a selected cell.

    Hi All,

    I want to come up with a macro which when a user will select a cell and run that macro then the macro will give the RGB numbers of the color pattern which cell has in a msgbox. I have the below code so far. Please expedite.

    Function RGB(CellRef As Variant)
    RGB = ToHex(Range(CellRef).Interior.Color)
    End Function
    
    Function ToHex(ByVal N As Long) As String
    strH = ""
    For i = 1 To 6
    d = N Mod 16
    strH = Chr(48 + (d Mod 9) + 16 * (d \ 9)) & strH
    N = N \ 16
    Next i
    ToHex = strH
    End Function
    Thanks a lot for your help in advance.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: macro to show RGB numbers of a selected cell.

    Function CellColor(r As Range)
        CellColor = Hex(r(1).Interior.Color)
    End Function
    E.g., =CellColor(A1)
    Entia non sunt multiplicanda sine necessitate

  3. #3
    abhay_547
    Guest

    Re: macro to show RGB numbers of a selected cell.

    Hi Shg,

    Thanks a lot for your reply, But I want to get the RGB code and not Hex code.


    Thanks a lot for your help in advance.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro to show RGB numbers of a selected cell.

    Hello abhay_457,

    What is the difference between the proposed solution and your code?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    abhay_547
    Guest

    Re: macro to show RGB numbers of a selected cell.

    Hi Ross,

    Thanks a lot for your reply, I want to get the RGB color numbers and not Hex code. for e.g.

    Cell A1 is colored / filled with "Light Yellow" color then the RGB for the same will be R = 255, G = 255, B = 153. So I am looking for something which will show Msgbox RGB("A1").


    Thanks a lot for your help in advance.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro to show RGB numbers of a selected cell.

    Hello abhay_547,

    Perhaps a macro like this will do...
    Sub ShowRGB()
    
      Dim R As Integer, G As Integer, B As Integer
      Dim N As String
      
        N = Hex(ActiveCell.Interior.Color)
        
          R = Val("&H" & Left(N, 2))
          G = Val("&H" & Mid(N, 3, 2))
          B = Val("&H" & Right(N, 2))
        
        MsgBox "Active Cell Color: R = " & R & ", G = " & G & ", B = " & B
        
    End Sub

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,974

    Re: macro to show RGB numbers of a selected cell.

    What exactly are you expecting the output to be?
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: macro to show RGB numbers of a selected cell.

    Does this help?

    Function GetRGB(rng As Range)
        Dim strHEX As String
    
        strHEX = Right("000000" & Hex(rng.Interior.Color), 6)
        GetRGB = WorksheetFunction.Hex2Dec(Right(strHEX, 2)) & ", " & _
                 WorksheetFunction.Hex2Dec(Mid(strHEX, 3, 2)) & ", " & _
                 WorksheetFunction.Hex2Dec(Left(strHEX, 2))
    
    End Function

    Enter so
    =GetRGB(A2)

    [EDIT]
    Where did all these replies come from???...
    Last edited by Marcol; 11-05-2010 at 07:04 AM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,974

    Re: macro to show RGB numbers of a selected cell.

    RGB values are always the same for a given colour. However, if you move a file from 2007 to 2003, the colours themselves may change, so the RGB values will be different.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1