+ Reply to Thread
Results 1 to 11 of 11

Simple IF statement not working

  1. #1
    Registered User
    Join Date
    11-12-2017
    Location
    Stafford, England
    MS-Off Ver
    365
    Posts
    184

    Simple IF statement not working

    Hi,

    This should probably be easy for me to work out but I haven't managed to.

    WB has background image to color entire WB so cell interior color = none

    Using the following code I want to leave cell color as none if it is none otherwise alternate between the colors in the ElseIf & Else statements.

    Please Login or Register  to view this content.
    Currently the ElseIf & Else statements work fine but they also work if cell color is none so the IF statement isn't working as it should

    Please Login or Register  to view this content.
    selects first 7 columns of selected row which I want to keep

    Please Login or Register  to view this content.
    selects first cell of selected row which I also want to keep

    Anyone know where this code is going wrong?

    Thanks

  2. #2
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Simple IF statement not working

    If you insert the following statement before the If-statement for debugging purposes, you should see your mistake:

    MsgBox xlNone & vbNewLine & Selection.Interior.Color & RGB(255,255,255)

    Alternatively, if you know how to use the Immediate Window at a breakpoint on the If-statement, enter:

    ? xlNone
    ? Selection.Interior.Color
    ? RGB(255,255,255)

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Simple IF statement not working

    xlNone is not a value that a cell's .Color property can take. It is a value that the .ColorIndex property might take.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    11-12-2017
    Location
    Stafford, England
    MS-Off Ver
    365
    Posts
    184

    Re: Simple IF statement not working

    Thanks for your reply, I am somewhat of a novice so do not know about the Immediate Window or breakpoint and I don't understand the information in the message box that pops up... Sorry

    Still learning and trying to work things out

  5. #5
    Registered User
    Join Date
    11-12-2017
    Location
    Stafford, England
    MS-Off Ver
    365
    Posts
    184

    Re: Simple IF statement not working

    Sample attached with background image used

    Clicking highlight in the white area should highlight white/yellow alternately which it does

    Outside of white area should do nothing but it also highlights white/yellow
    Attached Images Attached Images
    Attached Files Attached Files

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Simple IF statement not working

    the IF statement isn't working as it should
    When something isn't working the way you expect, describe 1) What you expect 2) What is happening instead. I think you may have multiple issues tangled up here and I suspect that something you think is working is not actually working.

    xlNone is for ColorIndex, not Color. ColorIndex is an index into the color palette, and has a value of 1-56. If it's value is xlNone, it means no color is assigned to that item. The value of xlNone is -4142.

    Color is a Long number that represents the RGB value of the color from 0 - 16,777,215.

    Therefore this condition

    Please Login or Register  to view this content.
    will always be False.

    If you refer to the Interior.Color of a Range, and the cells in the range different colors, the value of Color will be 0. I don't know what your cells look like so I can't say if this is an issue with your worksheet.

    In the second condition, you are not setting the color to "no color," you are setting it to solid fill with RGB(255, 255, 255) which is white. They are not the same thing. If you want to clear the color to "no color" use

    Please Login or Register  to view this content.
    The Color property will still be RGB(255,255,255) but the fill is cleared.

    Also I strongly advise you to program without GoTo statements to the greatest extent possible. As it happens, in the code you showed here that GoTo has no effect anyway. If you deleted that line of code, the logic would still flow the same way.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Simple IF statement not working

    Seeing your file helps. I believe this code does what you want. Give it a try.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Simple IF statement not working

    Quote Originally Posted by ~TaC~ View Post
    I am somewhat of a novice so do not know about the Immediate Window or breakpoint
    Really powerful tools to learn when working with VBA. Sorry, I don't know a good reference to teach you.

    -----

    Quote Originally Posted by ~TaC~ View Post
    I don't understand the information in the message box that pops up
    Can you see that the value of xlNone is different from the value of Selection.Interior.Color?

    On the contrary, can you see that the value of Selection.Interiod.Color is the same as RGB(255,255,255)?

    That is all you should need to understand. From that, you should realize that the correct If-statement is (one way):

    If Selection.Interior.Color = RGB(255,255,255) Then

    Even better (improved programming structure):

    Please Login or Register  to view this content.
    No need for the GoTo statement and Skip label.

    That should correct the problem as explained, namely with If-statement part per se . I cannot say that does what you intended.
    Last edited by joeu2004; 12-26-2020 at 06:55 PM.

  9. #9
    Registered User
    Join Date
    11-12-2017
    Location
    Stafford, England
    MS-Off Ver
    365
    Posts
    184

    Re: Simple IF statement not working

    Quote Originally Posted by 6StringJazzer View Post
    Seeing your file helps. I believe this code does what you want. Give it a try.

    Please Login or Register  to view this content.
    Perfect result thank you.

    I've spent days trying to work it out and now its written can see where I was going wrong.
    It looks somewhat simple as I knew it would be.

    Its frustrating as a novice trying to explain what I want and thinking I am explaining it right but it not coming across to you guys quite right.

    I do try to work things out before coming here but my knowledge is limited

    Works as intended, Thank you for the lesson

  10. #10
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Simple IF statement not working

    Quote Originally Posted by ~TaC~ View Post
    Clicking highlight in the white area should highlight white/yellow alternately which it does[.] Outside of white area should do nothing but it also highlights white/yellow
    My understanding is:

    1. If the color is white or yellow, each click should alternate between the two.
    2. If the color is neither white nor yellow, each click should do nothing.

    If that understanding is correct, your original code (as intended) was wrong, to begin with.

    The code that 6StringJazzer offered might do what you intended, as I understand it above. Alternatively:

    Please Login or Register  to view this content.
    PS.... If you do not want the alternation to start when there is no color at all, we could adapt the suggestion by 6StringJazzer, to wit:

    Please Login or Register  to view this content.
    Last edited by joeu2004; 12-26-2020 at 07:15 PM.

  11. #11
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Simple IF statement not working

    Please Login or Register  to view this content.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 08-29-2019, 04:38 PM
  2. [SOLVED] If statement on working out percentage and based on time and date not working.
    By Johnny247 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-11-2019, 06:07 PM
  3. multiple if statement not working but not working (make sense!)
    By vanessafvg in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-17-2018, 12:06 AM
  4. Well working simple macro now not working-open folder
    By jomili in forum Word Programming / VBA / Macros
    Replies: 6
    Last Post: 07-05-2017, 08:24 AM
  5. [SOLVED] Simple nested IF statement not working correctly
    By bassemsaad in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-04-2013, 08:53 AM
  6. [SOLVED] Simple IF Statement is not working (or I've lost my mind)
    By hilarypedder in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-02-2013, 05:45 PM
  7. [SOLVED] Help with simple IF statement
    By timohteee in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-22-2013, 10:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1