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
Solved

Problem with Conditional Formatting Macro

Posted on 2013-11-28
8
324 Views
Last Modified: 2013-11-29
Folks,
When you open the attached workbook and on the first tab click display data I'm trying to display the lowest value in the table excluding zero. I call a macro that executes the data to be displayed with Conditional Formatting. I cannot find anything wrong with the code, yet it highlights zero values which is incorrect. In the table the lowest nonzero value is 2, not zero.
NonZero.xlsm
0
Comment
Question by:Frank Freese
  • 3
  • 3
  • 2
8 Comments
 
LVL 22

Expert Comment

by:Flyster
ID: 39684614
This should take care of the zeros:
Sub LowestNonZero()
'
' LowestNonZero Macro
'

'
    Range("A2:D10").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
       "=A2=MIN(IF($A$2:$D$10<>0,$A$2:$D$10))"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Bold = True
        .Italic = False
        .Color = -16711681
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = -0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

Open in new window

Flyster
0
 
LVL 50

Accepted Solution

by:
barry houdini earned 500 total points
ID: 39685154
HelloFlyster - isn't that the same formula that's already there?

I think this is caused by a bug in conditional formatting in Excel 2007 - array formulas used in conditional formatting need to be "refreshed" before they work. When I opened the file I saw the zeroes formatted, if I go into conditional formatting > Edit > without changing the formula but hitting OK it then works and formats 2 as required.

If you can use a non-array formula for conditional formatting that will work without fail. For that you can use this formula:

=AND(A2<>0,COUNTIFS($A$2:$D$10,"<"&A2,$A$2:$D$10,"<>0")=0)

that will highlight the lowest value if it's not zero. Just put that in quotes in your macro in place of the current formula, with quotes doubled-up, i.e.

"=AND(A2<>0,COUNTIFS($A$2:$D$10,""<""&A2,$A$2:$D$10,""<>0"")=0)"

regards, barry
0
 

Author Comment

by:Frank Freese
ID: 39685548
barry,
You correct in that Flyster's fromula was the same as I had submitted.
Your first suggestion was rejected with a Compiled error: Expression expected.
However, the second alternative worked.
What is confusing is when I use the code I provided in a macro it does not give the expected results. However, if I do not use a macro, it worked just fine.
Any thoughts on that before I award you points?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 50

Expert Comment

by:barry houdini
ID: 39685620
Do you mean if you set up the conditional formatting manually (with the original formula) it works OK?

When I have seen this problem in the past the conditional formatting works OK when initially applied ......but then it either does nothing or gives wrong results when the workbook is closed and re-opened - the CF formula remains there and if you go in to CF as if to edit, but leave the formula the same, it then works OK again.

I tried searching for more details but I can't find a good link

regards, barry
0
 
LVL 22

Expert Comment

by:Flyster
ID: 39685648
@ Barry,

You're right. I don't know what happened but it is the same. The one I ran prior to submitting was highlighting the 2 in B5. I'll have to check it when I get back to the office. My apologies to fh_freese!

Paul
0
 
LVL 50

Expert Comment

by:barry houdini
ID: 39685709
There's nothing intrinsicly wrong with that formula, Paul. It's an excel bug that stops it working as it should - I don't think that bug exists outside 2007 so if you tested in another version it may have worked perfectly.

regards, barry
0
 

Author Comment

by:Frank Freese
ID: 39685710
Barry,
When I set up the Conditional Format manually no problem. When I duplicated the same thing to create the macro I got the wrong results.
Paul, I do appreciate you work, however, I'll need to accept the solution Barry offered.
I do appreciate everyone's help.
0
 

Author Closing Comment

by:Frank Freese
ID: 39685713
thank you very much
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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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…

856 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