+ Reply to Thread
Results 1 to 11 of 11

Cell Shading Macro Depending on Text

Hybrid View

BluTalon Cell Shading Macro Depending... 05-30-2008, 07:19 PM
rylo Hi Try this Sub aaa()... 05-30-2008, 07:42 PM
Shijesh Kumar Hi. Try this code, it... 05-30-2008, 07:45 PM
Leith Ross Hello BluTalon, This... 05-30-2008, 08:11 PM
BluTalon oops. I'm sorry. Both seem... 05-30-2008, 08:32 PM
jindon Sub test() Dim i As Long,... 05-30-2008, 09:23 PM
Leith Ross Hello BluTalon, To skip... 05-30-2008, 09:28 PM
royUK Why not use Conditional... 05-31-2008, 03:24 AM
  1. #1
    Registered User
    Join Date
    01-10-2008
    Posts
    44

    Cell Shading Macro Depending on Text

    Hi there.

    I will have a spreadsheet similar to the image provided. Basically, I need the macro to go row by row down column I. If the cell in column I contains the word "Tech" I need the cells from column B to P to be shaded color1; if it contains "Update," color 2; if the cell is blank; leave the cell as is. Once the macro has done this, it will continue to the next row and to the same check.

    The number of rows in the spreadsheet will change on a day-to-day basis depending on the data that's pulled, but the columns should remain the same.

    So I think I have the shading part. This is what I have so far. I'm not sure if this is the most efficient way of coding it, but it kinda works. I guess another problem would be if in the future the column with this info (column I) changes and shifts.


    Sub Shading()
    
    ActiveSheet.Range("I3").Select
    
    
    If Range("I3") = "Tech" Then
        Range("C3:P3").Select
        With Selection.Interior
            .ColorIndex = 36
            .Pattern = xlSolid
        End With
    ElseIf Range("I3") = "Updates" Then
        Range("C3:P3").Select
        With Selection.Interior
            .ColorIndex = 34
            .Pattern = xlSolid
        End With
    End If
    
    End Sub

    I've been trying to figure something out, but I haven't been able to wrap my brain on how to do this.

    Thanks to all for any help you can provide.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try this

    Sub aaa()
      For Each ce In Range("I3:I" & Cells(Rows.Count, "I").End(xlUp).Row)
        Select Case ce.Value
          Case "Tech"
            colr = 36
          Case "Update"
            colr = 34
          Case Else
            colr = xlNone
        End Select
        Cells(ce.Row, "B").Resize(1, 15).Interior.ColorIndex = colr
      
      Next ce
    End Sub
    rylo

  3. #3
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717
    Hi.

    Try this code, it should work.

    I have made the assumption that column "A" will definitely contain some value so the macros will do its work until it encounter a blank in column "A"


    Sub COLOR_ROWS()
        Dim r As Long
        Dim c As Integer
        
        ' r is the row where data starts
        ' c is column
        
        r = 3
        c = 9
        
        'have taken into consideration that column a will contain some value
        
        Do While Not IsEmpty(Cells(r, 1))
            
            If Cells(r, c) = "Tech" Then
                Range(Cells(r, 2), Cells(r, 16)).Interior.ColorIndex = 36
            ElseIf Cells(r, c) = "Update" Then
                Range(Cells(r, 2), Cells(r, 16)).Interior.ColorIndex = 34
        
            End If
           
            r = r + 1
            
        Loop
        
    End Sub

  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
    Hello BluTalon,

    This version automatically "reads" the size of the your array using the first cell ($A$3) as a reference. It allows you to use more colors easily later on.
    Sub Shading()
    
      Dim CI As Long
      Dim R As Long
      Dim Rng As Range
      
        Set Rng = ActiveSheet.Range("$A$3").CurrentRegion
        
          For R = 1 To Rng.Rows.Count
            Select Case Rng.Cells(R, 9)
              Case Is = "Tech"
                CI = 36
              Case Is = "Update"
                CI = 34
              Case Else
                CI = xlNone
            End Select
            With Rng
              .Range(Cells(R, 1), Cells(R, .Columns.Count)).Interior.ColorIndex = CI
            End With
          Next R
        
    End Sub
    Sincerely,
    Leith Ross

  5. #5
    Registered User
    Join Date
    01-10-2008
    Posts
    44

    Red face

    oops. I'm sorry. Both seem to work, but I forgot that it also has a header row that automatically gets created when the spread sheet is exported.

    So, the header should not be edited, but it does contain "Tech" in one of the cells. This of course kinda screws up the way the macro formats the cells as the header turns a different color. Is there a way we can have it start below the header?

    Thanks and sorry again
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    Sub test()
    Dim i As Long, myClr As Long
    With Range("a1").CurrentRegion
        With .Resize(, .Columns.Count - 1).Offset(,1)
            For i = 2 To .Rows.Count
                Select Case .Rows(i).Range("h1").Value
                    Case "Tech" : myClr = 36
                    Case "Update" : myClr = 34
                    Case Else : myClr = xlNone
                End Select
                .Rows(i).Interior.ColorIndex = myClr
            Next
        End With
    End With
    End Sub

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

    To skip the header (1st row in the array), The "For R" loop needs to start at 2. I have made the change already. You can also change the column to check by changing the variable "C".
    Sub Shading()
    
      Dim C As Long
      Dim CI As Long
      Dim R As Long
      Dim Rng As Range
      
        C = 9   'Column "I"
    
        Set Rng = ActiveSheet.Cells(StartRow, C).CurrentRegion
        
          For R = 2 To Rng.Rows.Count
            Select Case Rng.Cells(R, C)
              Case Is = "Tech"
                CI = 36
              Case Is = "Update"
                CI = 34
              Case Else
                CI = xlNone
            End Select
            With Rng
              .Range(Cells(R, 1), Cells(R, .Columns.Count)).Interior.ColorIndex = CI
            End With
          Next R
        
    End Sub
    Sincerely,
    Leith Ross

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Why not use Conditional Formatting?

    See

    http://www.excel-it.com/excel_condit...formatting.htm
    Last edited by royUK; 05-31-2008 at 05:57 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  9. #9
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717
    opps...

    It totally slipped out of my mind...


    Yes we can use conditional formating...

    That would be easy as well to use

+ 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