Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cell color

Posted on 2013-05-09
10
Medium Priority
?
328 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
[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
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 49

Assisted Solution

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

Accepted Solution

by:
Martin Liss earned 1000 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 500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Assisted Solution

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

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
 

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 49

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 49

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

598 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