+ Reply to Thread
Results 1 to 3 of 3

Changing background colour depending on value

Hybrid View

rahulpandita Changing background colour... 12-18-2010, 01:48 PM
Leith Ross Re: Changing background... 12-18-2010, 08:36 PM
rahulpandita Re: Changing background... 12-19-2010, 12:19 AM
  1. #1
    Registered User
    Join Date
    12-18-2010
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    7

    Changing background colour depending on value

    I have 2 sheets – Data & RequiredFormat.

    I need to color the ASINS at every change in Coll_Var with Orange & Blue. While coloring the internal child asins with lighter and darker shades of the same colour as the Parent.

    Let me know if it makes sense or you need more inputs! Thanks for your help with this. We can make a reusable component if you build a flexible code.
    Attached Files Attached Files

  2. #2
    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: Changing background colour depending on value

    Hello Rahulpandita,

    Welcome to the Forum!

    The attached workbook contains the macro shown here. It will format the data as you specified.
    'Written: Decemeber 18, 2010
    'Author: Leith Ross  (www.excelforum.com)
    
    Sub FormatData()
    
      Dim BaseColor(1) As Integer
      Dim ColorFlag As Long
      Dim Group As Range
      Dim HighLight(1) As Integer
      Dim I As Long
      Dim LastColumn As Long
      Dim LastRow As Long
      Dim N As Long
      Dim R As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        Set Wks = Set Wks = Worksheets("Data")
        
        LastColumn = Wks.Cells(1, Columns.Count).End(xlToLeft).Column
      
        Set Rng = Wks.Range("A2", Wks.Cells(2, LastColumn))
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
        
          I = 1
          BaseColor(0) = 40
          BaseColor(1) = 20
          HighLight(0) = 44
          HighLight(1) = 37
          
          For Each Cell In Rng.Columns(1).Cells
            N = N + 1
            If Cell <> Cell.Offset(1, 0) Then
               ColorFlag = Abs(ColorFlag) Mod 2
               Set Group = Wks.Range(Rng.Cells(I, "A"), Rng.Cells(N, LastColumn))
               Group.Interior.ColorIndex = BaseColor(ColorFlag)
                 Set Group = Wks.Range(Rng.Cells(I, "B"), Rng.Cells(N, LastColumn))
                 For R = 1 To Group.Rows.Count Step 2
                   Group.Rows(R).Cells.Interior.ColorIndex = HighLight(ColorFlag)
                 Next R
               I = N + 1
               ColorFlag = ColorFlag + 1
            End If
          Next Cell
        
    End Sub

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Attached Files Attached Files
    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!)

  3. #3
    Registered User
    Join Date
    12-18-2010
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Changing background colour depending on value

    Thanks a lot.. it really works)

+ 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