Solved

Excel VBA: dynamically shade rows based on value of a cell

Posted on 2009-04-10
9
856 Views
Last Modified: 2012-06-21
see attached.  I need the yellow rows to appear as shown.   the number of rows in the report can vary, so th emacro needs to be dynamic.
ee-samp2.xls
0
Comment
Question by:LeeStink
  • 3
  • 3
  • 2
9 Comments
 
LVL 21

Expert Comment

by:oleggold
ID: 24118788
0
 
LVL 21

Expert Comment

by:oleggold
ID: 24118797
"We use the =INDIRECT() function to get the value of column C for each row:
If the value is TRUE : =INDIRECT("C"&ROW())=TRUE
If the value is 1 : =INDIRECT("C"&ROW())=1"
0
 

Author Comment

by:LeeStink
ID: 24118821
I dont need to shade the whole row, just the row beginning with column C..

thanks for the reponse.
0
 
LVL 21

Expert Comment

by:oleggold
ID: 24118828
sorry ,here"
Excel has a great built-in function to achieve this: Conditional Formatting (In the menu Format » Conditional formatting). The first time it might be a little difficult to find your way to do this but if you spend a little time with it you will soon realize it is powerful function.

In the following example if we want the rows to be colored blue when column C is true and orange if the value is 1, we can use the following formula's:
Select all the cells that need to be colored. Usually we color a cell based an its value and use the [Cell Value Is ] in the conditional formatting box. To format cells based on other cells you need the [Cell Formula Is].

"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:LeeStink
ID: 24118964
it has to be a macro...  it needs to be automated.
0
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 24119022
I dont see the rows shaded to yellow..What is the condition that is meet when you want to shade them..? also do you want to shade  data from C Column to AG Column only..??
0
 

Author Comment

by:LeeStink
ID: 24130296
rows where the value of column C are "Target" or "Non-target" need to be shaded yellow, and all cells to the right of the cell need to also be shaded, so yes, the entire row, but not the cells to the left of column C.

Thank you.
0
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 500 total points
ID: 24131214
Basis of your example, You can use the following code and it will do what you are looking for...
Saurabh...

Sub highlight()

    Dim rng As Range, lcol As Long, lrow As Long, CELL As Range

    lcol = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column

    lrow = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
 

    Set rng = Range("C6:C" & lrow)
 

    For Each CELL In rng

        If CELL.Value = "Target" Or CELL.Value = "Non-Target" Then

            Range("C" & CELL.Row & ":" & Cells(CELL.Row, lcol).Address).Interior.ColorIndex = 36

        End If

    Next CELL
 

    MsgBox "Done"
 
 
 

End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Formula 4 28
Sum iF  based on a null cell 11 29
Excel 2016: auto-number column of merged cells. 47 31
remove lower case characters in excel formula 12 43
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now