Solved

Cell color

Posted on 2013-05-09
10
295 Views
Last Modified: 2013-05-14
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
Comment
Question by:leezac
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 46

Assisted Solution

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

Accepted Solution

by:
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

by:byundt
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

Open in new window

0
 
LVL 32

Assisted Solution

by:Rob Henson
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 32

Expert Comment

by:Rob Henson
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
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:leezac
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 46

Expert Comment

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

Author Comment

by:leezac
ID: 39155763
I also do not want row colored - just cell
0
 
LVL 46

Expert Comment

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

Author Comment

by:leezac
ID: 39166334
Decided to use a "0' instead of '-" and it works now with conditional formatting.
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

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

914 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

12 Experts available now in Live!

Get 1:1 Help Now