+ Reply to Thread
Results 1 to 6 of 6

Finding the Interior Color value of a cell using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Finding the Interior Color value of a cell using VBA

    Hi there guys,

    Firstly, I have done many Google searches for this, i'm not sure if my terminology is wrong or if I'm too much of a rookie to figure out what i'm seeing, but I'm unable to use what I find in my own project. I hope someone can help.

    In Sheet1 cells A1-BW46 (3450 cells), using 9 different colors to fill all the cells.

    In Sheet3, starting in A2, I have a plain text list of 3450 cell ranges "A1", "A2", "A3" etc covering all 3450 cells in Sheet1.

    I want to populate next to this list the interior color value of the corresponding cell. So in cell A2, I will have the text "A1", in cell B2 I would have either a numerical or text value giving me the color that A1 in sheet1 is filled with.

    • The script doesn't have to auto update, I will run manually when needed
    • I don't need any manipulation, only the value of the color, and that can be hex, rgb or any other way
    • I'd be happy with a complete script obviously, but pointing me in the right direction would also be much appreciated!


    Sorry if this isn't clear. I can't upload the actual sheet I'm working with, but if my description of the problem is lacking, I may be able to create a sample sheet to give the idea.

    Many thanks in advance guys

    Darth269
    Last edited by Darth269; 02-17-2014 at 04:10 PM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Finding the Interior Color value of a cell using VBA

    This should do B2:
    With Sheet3.Range("B2")
        .Value = Sheet1.Range(.Offset(, -1)).Interior.ColorIndex
    End With
    Or for a range:
    Sub x()
    
    Dim r As Range
    
    For Each r In Sheet3.Range("B2:B4")
        r.Value = Sheet1.Range(r.Offset(, -1)).Interior.ColorIndex
    Next r
    
    End Sub
    Though ragulduy's approach is better.
    Last edited by StephenR; 02-17-2014 at 12:03 PM.

  3. #3
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Finding the Interior Color value of a cell using VBA

    You can get the colour index of a cell's interior from:
    Range("A1").Interior.Colorindex

    You could probably write a custom function along the lines of
    Public Function Cell_Interior_Colour(rng as range)
    Cell_Interior_Colour = rng.interior.colorindex
    End Function
    and then in B2:
    =Cell_Interior_Colour(A2)

    and copy down.

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Finding the Interior Color value of a cell using VBA

    Be careful of multiple cell ranges passed to the function, though:

    Function CellColor(myCell As Range)
    CellColor = myCell.Cells(1, 1).Interior.ColorIndex
    End Function
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Finding the Interior Color value of a cell using VBA

    Not sure this does what you want so test on a copy first.

    Sub Darth269()
    Dim rcell As Range
    Sheets("Sheet3").Activate
    For Each rcell In Range("A1:A" & Range("A" & Rows.count).End(3)(1).Row)
        rcell.Offset(, 1).Value = Sheets("Sheet1").Range(rcell.Value).Interior.Color
    Next rcell
    End Sub
    Last edited by JOHN H. DAVIS; 02-17-2014 at 12:19 PM.

  6. #6
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Finding the Interior Color value of a cell using VBA

    Thank you to all who responded. When I checked and saw StephenR's reply (before you other kind folks), I got to work, and his answer got me to where I needed to be, so many thanks StephenR.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Find a duplicate and then use the duplicates interior.color to change cell color.
    By bopsgtir in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-01-2012, 03:54 PM
  2. Change Interior Color using Cell Value
    By PY_ in forum Excel General
    Replies: 4
    Last Post: 03-23-2011, 01:05 PM
  3. [SOLVED] Cell background color (interior color) setting not working
    By Martin E. in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-21-2006, 02:10 PM
  4. Quick question RE: Finding a cell by interior color
    By Celt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2006, 01:40 PM
  5. interior color of a cell
    By R.VENKATARAMAN in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-11-2005, 01:10 AM

Tags for this Thread

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