Solved

# Cell color

Posted on 2013-05-09
321 Views
I need to be able to highlight a cell if it contains a hyphen or dash "-".  This is what I have, but does not work. Thanks in advance!

Sheets("View").Activate

Range(Range("I1"), Range("I1").End(xlDown)).Select
For Each cell In Selection
If cell = "-" Then cell.EntireRow.Interior.ColorIndex = 4
Next cell
0
Question by:leezac
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2
• +1

LVL 47

Assisted Solution

Martin Liss earned 250 total points
ID: 39153886
If Instr(1, cell, "-") Then cell.EntireRow.Interior.ColorIndex = 4
0

LVL 47

Accepted Solution

Martin Liss earned 250 total points
ID: 39153891
Actually I guess I should ask what you mean when you say it doesn't work? Does anything happen? Do you want to color the row if the cell contains something like "blah-blah"?
0

LVL 81

Assisted Solution

byundt earned 125 total points
ID: 39154009
Are the hyphens appearing in the cell as a result of formatting? If so, you need to specify Cell.Text in your macro. If they are manually entered, your existing code should work as desired.

The following modification of your code tests Cell.Text and uses InStr as suggested by MartinLiss.
``````Sub test()
Dim cell As Range, rg As Range
With Sheets("View")
Set rg = Range(.Range("I1"), .Range("I1").End(xlDown))
For Each cell In rg.Cells
If InStr(1, cell.Text, "-") > 0 Then cell.EntireRow.Interior.ColorIndex = 4
Next
End With
End Sub
``````
0

LVL 33

Assisted Solution

Rob Henson earned 125 total points
ID: 39155118
You could also achieve the formatting with Conditional Formatting with a custom CF with formula:

Select the required cells (assuming column I) and choose the Conditional Formatting wizard and enter the following as a formula condition:

=FIND("-",I1,1)>0

Thanks
Rob H
0

LVL 33

Expert Comment

ID: 39155127
Just realised you wanted the whole row coloured, so slight change.

Select the whole area and the CF formula would then be:

=FIND("-",\$I1,1)>0

Only change being the addition of the \$ in front of I to ensure that CF in all columns are looking at Column I.

Thanks
Rob H
0

Author Comment

ID: 39155619
No nothing works.  I entered the hypen manually to test but it is entered via a macro if the cell is null.  I tried conditional formating but it is deleted somehow when the file is updated.
0

LVL 47

Expert Comment

ID: 39155681
Can you attach your workbook and describe the steps to take to reproduce the problem?
0

Author Comment

ID: 39155763
I also do not want row colored - just cell
0

LVL 47

Expert Comment

ID: 39159906
Can you attach your workbook and describe the steps to take to reproduce the problem?
0

Author Comment

ID: 39166334
Decided to use a "0' instead of '-" and it works now with conditional formatting.
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

VBA routine modification/tweak needed 10 52
Modify Text File with Excel Macro 13 46
drag and drop a row in excel 2010 5 44
Excel - IF criterion 2 22
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
###### Suggested Courses
Course of the Month7 days, 14 hours left to enroll