+ Reply to Thread
Results 1 to 9 of 9

macro: changing color of coloumn header based on rows color

  1. #1
    Registered User
    Join Date
    01-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    8

    macro: changing color of coloumn header based on rows color

    Hi Experts,

    I wrote a macro to color the cell values in the rows based on their average value. For eg if the cell value is less than 0.2 Avg, they should be red color,if value is between 0.2 and 0.5 it should be yellow. This part is working fine

    Now based on the color of the rows cells , need to write a macro for the header one. Logic is Coloum header should be in red colour, if in one or more number of rows cells are red. same with yellow ones. Could you please help me out in solving this with logic.

    Thanks & Regards,
    Raj
    Last edited by rajaid; 01-27-2009 at 08:30 AM.

  2. #2
    Registered User
    Join Date
    03-16-2004
    Location
    UK
    MS-Off Ver
    2003
    Posts
    85

    Wink

    Somthing like this...

    Please Login or Register  to view this content.
    i.e. it counts the number of cells in the range with either red or yellow text in the variables r and y respectively. If either is > 0 then sets the colour of the header accordingly.

    You could do it all using conditional formatting of course. For the second bit you might want an array formula.
    ={IF(A2:A5>0.2,IF(A2:A5<0.5,"Title ","Title "),"Title")}
    And format based on "Title ", "Title " or "Title" I.e. the spaces being critical.

    Hope that helps...
    Last edited by incjourn; 01-15-2009 at 11:31 AM.
    Phil

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi, This code counts the number of coloured cells in each column, and then colours th first cell in that column with the same colour as the largest number.
    There must be a value below or in the last coloured cell of ec column or the code will miss the cell.
    You can change the selection to an explicit range if you require.
    Columns are checked up to the last value in row (1).
    Please Login or Register  to view this content.
    Regards Mick

  4. #4
    Registered User
    Join Date
    01-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    8
    Hi,

    Thanks for the solutions. I tried to implement based on the solutions received, but i am facing problem when ever the cell value is Green.

    To brief, the rows cells are colored based on conditional formatting . Values are Red and Green.

    Based on the color of the cell, corresponding header row should have that color.

    Samle Coding :
    Please Login or Register  to view this content.
    First, based on conditional formatting i am getting colors of the Rows cell, then reading each cell to find out is the color is red or green. It is working fine for Red, but some how there is a problem in reading yellow color cell even though that cell color is present.

    I pasted the code that i am using. Could you please look and help me if i am doing any mistake.

    KR,
    Raj
    Last edited by VBA Noob; 01-19-2009 at 02:29 PM.

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi, You will find the colours inserted using conditional Formatting, although they looks the same, they do not relate to the colorindex Numbers. It would be easier to write some code that initially coloured the cells using Colorindex Numbers and then use some code to check the numbers of coloured cells.
    Regards Mick

  7. #7
    Registered User
    Join Date
    01-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    8
    Hi,

    I tried of coloring cell rows by using VB code instead of conditional formatting, but there is some wrong in the code.
    Please Login or Register  to view this content.
    Values are present from B2 to H12. Coloumn I is the average of the corresponding row. Need to color the rows based on Avg

    I am not so much familiar with Vb code, could you please help me out.

    KR,
    Raj

  8. #8
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi, Try this:-
    The first code colours the cells in range "B2:H12", Red or Yellow Based on:- (0.2 x the value in the same row column "I").
    The second Code will colour the individual Column Headers (Row 1), Red or Yellow based on the Greater Number of Red or Yellow Cells in each column.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Regards Mick

  9. #9
    Registered User
    Join Date
    01-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    8
    Hi Mick,

    Thanks so much for the code. It is working fine.

    Thanks & Regards,
    Pradeep

+ Reply to 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