if elseif statements

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » If ElseIF Statements + And

If ElseIF Statements + And

resolvedResolved · High Priority · Version 2010

Sif has attended:
Excel Advanced course

If ElseIF Statements + And

Hi there

I am trying to do If... ElseIf statement but cannot work out how to do these including and 'And' (I want to change the value of the cell AND the colour).

This is the code I have:

If (rngTrim <> ".") Then

If rngTrim > highPTile Then
rngTrim.Value = newHigh And rngTrim.Interior.ColorIndex = 37
ElseIf rngTrim < lowPTile Then
rngTrim.Value = newLow
End If


It is currently giving me 0s instead of returning the 'new' value (newHigh)

Thank you in advance!

RE: If ElseIF Statements + And

Hello Sif,

Thank you for your question. The AND and the OR functions are really meant to use in your IF or ELSEIF lines, to run more than one test on a cell. If you want to apply more than one action on a condition, rather write these expressions on separate lines.

I would suggest:

If rngTrim > highPTile Then
rngTrim.Value = newHigh
rngTrim.Interior.ColorIndex = 37

ElseIf rngTrim < lowPTile Then
rngTrim.Value = newLow

End If

I hope this works.

Kind regards
Marius Barnard
Best STL

Fri 7 Aug 2015: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Using Alt in Save Dialog Box

When you are saving (or opening) a file, try these;
(Note the numbers are on the keyboard not the numeric keypad)
1. Go to previous folder Alt+1
2. Go up one folder level Alt+2
3. Search the Web Alt+3
4. Delete selected file Alt+4 or Delete
5. Create a new folder Alt+5
6. Cycle through all views Alt+6 repeatedly
7. Display the Tools menu Alt+7

View all Excel hints and tips


Server loaded in 0.08 secs.