+ Reply to Thread
Results 1 to 8 of 8

ColorIndex works but RGB doesn't

  1. #1
    Registered User
    Join Date
    02-12-2020
    Location
    Cincinnati, OH
    MS-Off Ver
    2017
    Posts
    6

    Question ColorIndex works but RGB doesn't

    Hi. I wrote a code that goes through the rows of a every sheet in a workbook and finds the ones that have one of two colors, then copies that needed data from that row and pastes it into another tab that was created earlier in the macro. Essentially, it skips a row unless it's highlighted yellow (ColorIndex = 6) or a pinkish color, RGB(230, 184, 183). My macro works for the ColorIndex, but not for the RGB, no matter what the ColorIndex or RGB values are (I've tried different ones to be sure). I've even tried not doing it as an 'Or' and just doing the ColorIndex (which works) or just the RGB (which doesn't work). Am I not using the RGB correctly? See the relevant code below:

    For j = 1 To sheetnum
    For i = 5 To 100
    Sheets(j).Activate
    If Cells(i, 5).Interior.ColorIndex = 6 Or Cells(i, 5).Interior.Color = RGB(230, 184, 183) Then 'Looks for yellow and red cells
    Cells(i, 4).Select
    Range(Cells(i, 4), Cells(i, 11)).Copy
    Sheets("Sourcing").Activate 'Switches to the Sourcing tab
    ActiveSheet.Paste Destination:=Worksheets("Sourcing").Range(Cells(n, 2), Cells(n, 11))
    'QT.Activate 'Goes back to QT sheet
    n = n + 1 'next row
    order = order + 1 'next order number
    End If
    Next i
    Next j

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: ColorIndex works but RGB doesn't

    Hi & welcome to the board.
    Select one of the cells that have the pink fill & run this
    Please Login or Register  to view this content.
    What number does the msgbox show?

  3. #3
    Registered User
    Join Date
    02-12-2020
    Location
    Cincinnati, OH
    MS-Off Ver
    2017
    Posts
    6

    Re: ColorIndex works but RGB doesn't

    The number in the msgbox is 10079487

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,302

    Re: ColorIndex works but RGB doesn't

    RGB(230,184,183) should be 12040422
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-12-2020
    Location
    Cincinnati, OH
    MS-Off Ver
    2017
    Posts
    6

    Re: ColorIndex works but RGB doesn't

    You're right. I think I know what is happening but not exactly why. 1240422 matches the cell background color for most of the cells. It's the sheet's default cell color. However, there is a conditional formatting that changes some of those cells to the RGB(230,184,183). When I change my code to:

    Cells(i, 5).Interior.Color = 1240422

    it finds nothing, just like the RGB. But when I change it to 10079487, if finds ALL of the cells, even the ones whose color has been changed by the conditional formatting.

    How do we get it to see the conditional formatted color?

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: ColorIndex works but RGB doesn't

    If it's set by CF then you need to use
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-12-2020
    Location
    Cincinnati, OH
    MS-Off Ver
    2017
    Posts
    6

    Re: ColorIndex works but RGB doesn't

    Yes! Thank you. You were such a big help with figuring out the root of the problem. Thanks again

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: ColorIndex works but RGB doesn't

    You're welcome & thanks for the feedback

+ 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. Code F8 works but F5 doesn't
    By matthollis881 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-15-2017, 12:51 PM
  2. Need help debugging an Add-In that sometimes works and sometimes doesn't
    By damjanovic in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-10-2014, 05:19 PM
  3. My VBA Macro Sometimes Works and Sometimes Doesn't
    By chsaav in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-22-2013, 09:10 PM
  4. Code that sometimes works and sometimes doesn't
    By kat14 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-06-2013, 05:20 PM
  5. if font.colorindex doesn't read conditional format
    By devile in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2011, 10:44 AM
  6. [SOLVED] Sub works sometimes and sometimes doesn't
    By Barry O''Connell in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-18-2006, 10:10 PM
  7. it works, it doesn work, its works....and so on.
    By Naz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-30-2005, 09:05 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