Review VBA code? What is this in English?

RWayneH
RWayneH used Ask the Experts™
on
Could I have someone review this code?  It just looked odd not having the Result =  in each case following Case statements after the original Case Is?  Would like to know what this is saying in English.

Range("D2").Activate
    Select Case ActiveCell.Value
    
        Case Is > 1.15 * IEHaworthLogin
            Result = 1.15 * IEHaworthLogin
            ActiveCell.Interior.Color = vbRed
        Case 1.1 * IEHaworthLogin To 1.15 * IEHaworthLogin
            ActiveCell.Interior.Color = vbYellow
        Case 0.9 * IEHaworthLogin To 1.1 * IEHaworthLogin
            ActiveCell.Interior.Color = vbGreen
        Case Is < 0.9 * IEHaworthLogin
            ActiveCell.Interior.Color = vbCyan
    End Select

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
The first one that's true is calculated and the following ones are ignored.
Range("D2").Activate
    Select Case ActiveCell.Value ' Examine the contents of D2
    
        Case Is > 1.15 * IEHaworthLogin
           ' It's greater than 1.15 * IEHaworthLogin (I assume that this is some kind of numeric variable)
            Result = 1.15 * IEHaworthLogin
            ActiveCell.Interior.Color = vbRed ' Make the cell red
        Case 1.1 * IEHaworthLogin To 1.15 * IEHaworthLogin
             'The value is between 1.11 * IEHaworthLogin and 1.15 * IEHaworthLogin
            ActiveCell.Interior.Color = vbYellow
        Case 0.9 * IEHaworthLogin To 1.1 * IEHaworthLogin
           'The value is between 0.9 * IEHaworthLogin and 1.11 * IEHaworthLogin
            ActiveCell.Interior.Color = vbGreen
        Case Is < 0.9 * IEHaworthLogin
          ' 0.9 * IEHaworthLogin value is < .9 so make it cyan color
            ActiveCell.Interior.Color = vbCyan
    End Select

Open in new window

Fabrice LambertConsulting
Distinguished Expert 2017
Commented:
In short:

Select the D2 cell.
If the cell's value is above 1.15 multiplied by IEHaworthLogin:
- Store 1.15 multiplied by IEHaworthLogin for later use.
- Turn the cell's color to red.

If the cell's value is between 1.1 * IEHaworthLogin and  1.15 * IEHaworthLogin:
- Turn the cell's color to yellow

If the cell's value is between 0.9 * IEHaworthLogin and 1.1 * IEHaworthLogin:
- turn the cell's color to green

If the cell's value is below 0.9 * IEHaworthLogin:
- turn the cell's color to cyan

And what IEHaworthLogin mean ? No clue ....

Side notes:
- Most if this code could be replaced by conditional formatting rules.
- It sux !
Activating cells is slow and disturb the user.
Using the ActiveCell object provide zero garantees.
Top Expert 2014
Commented:
1. I recommend deleting the Activate statement and changing the Select Case statement to the following:
    Select Case CDbl(Range("D2").Value)

Open in new window

or
    Select Case CSng(Range("D2").Value)

Open in new window

2. There is always a chance that the results might not be what you expect because of floating point arithmetic and representation.
3. What is the data type of the IEHaworthLogin variable?
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
It doesn't make sense to have Result = in the first Case or anywhere (if similar statements were added)  because Result is being set to what the value already is and you could use Range("D2").Value directly.

Author

Commented:
Looks like the data type in the variable is General... and it is an average.  I was at first concerned about the E+02 type values in the average variable but it is not there anymore.  I prefer the If and ElseIf method myself...  The sub is only checking a couple cells, so it is not a huge deal.. and I did not want to go down the Conditional Formatting path.

There was a comment by Martin that said "rest are ignored"?  Is that ok?  As long as Result is calculated once it is good? Or should that be in each of the follow Cases too?  I guess I am not used to the Case Is and Case method instead of the If and ElseIf

Author

Commented:
How would that look replacing the code with:  Range("D2").Value directly?  Or using the If and IfElse method instead of CaseIs and Case?
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I have no idea what Result is used for but MsgBox Range("D2").Value would show the same value that's in Result.

IMO, If and IF Else would be a poorer way to go.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
There was a comment by Martin that said "rest are ignored"?  Is that ok?
They are only ignored in the sense that once VBA finds a Case that's True the others aren't looked at, and I prefer Select Case over If's for that reason, because with If's every If is evaluated even if previous ones are True and that takes up unnecessary processing time (even if it's minuscule).
IT / Software Engineering Consultant
Top Expert 2016
Commented:
Based on the comments I think this is where I would end up.  Basically if the value is > 1.15 * IEHaworthLogin then Result is set to that, and the color is red.  The other parts of the case just set the cell to different colors depending on values.

I would mention that some boundary values like 1.1 * IEHaworthLogin are somewhat ambiguous currently, since they are technically included in two ranges.  So the first match will be taken, which may be okay for what you want...

    Select Case CDbl(Range("D2").Value)
        Case Is > 1.15 * IEHaworthLogin
            Result = 1.15 * IEHaworthLogin
            ActiveCell.Interior.Color = vbRed
        Case 1.1 * IEHaworthLogin To 1.15 * IEHaworthLogin
            ActiveCell.Interior.Color = vbYellow
        Case 0.9 * IEHaworthLogin To 1.1 * IEHaworthLogin
            ActiveCell.Interior.Color = vbGreen
        Case Is < 0.9 * IEHaworthLogin
            ActiveCell.Interior.Color = vbCyan
    End Select

Open in new window


»bp

Author

Commented:
Thanks all for the help today....  guess I need to start writing more Case Is \ Case statements then If and IfElse...   Appreciate the help.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017

Author

Commented:
Maybe I should add this note, that I had to remove the ActiveCell references too.

Based on this the final code looks like:
Select Case (Range("D2").Value)
    Case Is > 1.15 * IEHaworthLogin
        Result = 1.15 * IEHaworthLogin
        Range("D2").Interior.Color = vbRed
    Case 1.1 * IEHaworthLogin To 1.15 * IEHaworthLogin
        Range("D2").Interior.Color = vbYellow
    Case 0.9 * IEHaworthLogin To 1.1 * IEHaworthLogin
        ActiveCell.Interior.Color = vbGreen
    Case Is < 0.9 * IEHaworthLogin
        Range("D2").Interior.Color = vbCyan
End Select

Open in new window

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Do you need line 3?

Author

Commented:
Thats right...  I did not need Ln3.  Thanks for pointing that out.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial