Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

change cell colour row

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Change cell colour of row based upon text contained in a cell

Change cell colour of row based upon text contained in a cell

ResolvedVersion 2007

Ryan has attended:
Excel Advanced course
Excel VBA Intro Intermediate course

Change cell colour of row based upon text contained in a cell

Good afternoon,

Please can you help me?

I am trying to write a piece of code that will change the colour of the cells in a given row, based upon part of the text contained within a specific cell of the given row.

For example, the ninth column of my data may contain the words "move to" with a date, I would like to colour any rows where this text is contained within a cell to a particular colour.

I know how to do this if the string being searched is the entire cell content, but not only if it is contained within part of the cell.

Sorry for the waffle, I hope I have explained myself sufficiently.

Kind thanks,

Ryan

RE: Change cell colour of row based upon text contained in a cel

Hi Ryan, you don't need to do this with code you can do it simply with conditional formatting.

Select all your data excluding the headings

Click Conditional Formatting on the Home tab

Choose New Rule from the drop down

Choose "Use a formula to determine which cells to format"

In the field below enter:

=IF(ISNUMBER(SEARCH("*move to*",$I1)),TRUE, FALSE)

...altering $I1 to be the first cell in the field you are testing, keeping the absolute reference on the column

Click format and apply any formatting you want

Click OK and check if your conditional formatting has been applied

If it hasn't, it's probably due to a Microsoft bug. Go back to the Conditional formatting drop down and choose Manage Rules. Click on your rule and then on Edit Rule. See if the cell reference has been altered to something like $I1048576. change this back to $I1 and reapply. That should do the trick.

Hope this helps,

Anthony

RE: Change cell colour of row based upon text contained in a cel

Hi Anthony,

Thank you for your response, the reason that I wanted to do this via VBA is that the purpose of this was to automate the formatting of the data and as such would be part of a larger code.

Is there a way that this can be included in VBA?

Kind thanks,

Ryan

RE: Change cell colour of row based upon text contained in a cel

Hi Ryan. Code-wise there are many different ways of doing this. Probably the simplest is to leverage the Autofilter function:

*******************************

Sub FormatMe()

Selection.AutoFilter

ActiveSheet.Range("$A$1:$L$17").AutoFilter Field:=9, Criteria1:= _
"=*move to*", Operator:=xlAnd

ActiveCell.CurrentRegion.Select

Selection.Interior.Color = vbRed

ActiveSheet.ShowAllData

Selection.AutoFilter

Range("A1").Select


End Sub

*******************************

Hope this helps,

Anthony

Tue 8 May 2012: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Adding Rows or Columns in an Excel 2010 Worksheet

If you want to add a row to an Excel spreadsheet, these are the simple steps you should take:

With your mouse, right click on the row header below where you want the new row to be added. Then, click Insert.

Follow exactly the same steps if you want to add a column to an Excel worksheet, right click on the column header, choose Insert and the new column will be inserted to the left of the selected column.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.