Results 1 to 9 of 9

Thread: If then else not working
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Mar 2018
    Posts
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default If then else not working

    I am trying to add what I thought would be an easy addition to my code.
    I have two macros that calculate using different formulas.
    I want to automatically call the right macro if the cell 2 rows above the active cell is "W"
    Although the value is "W" the macro jumps straight to the else statement and calls the wrong macro.
    [code]
    Sub Wednesday()
    Range("graynext").Select
    If ActiveCell.Offset(-2, 0) = "W" Then
    Call PasteandcalcSat
    Else
    Call PasteAndDown
    End If
    End Sub
    [\code]

  2. #2
    Board Regular lrobbo314's Avatar
    Join Date
    Jul 2008
    Location
    California
    Posts
    2,403
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)

    Default Re: If then else not working

    Changed ActiveCell.Offset(-2, 0) to ActiveCell.Offset(-2, 0).Value

    Code:
    Sub Wednesday()
    Range("graynext").Select
    If ActiveCell.Offset(-2, 0).Value = "W" Then
    Call PasteandcalcSat
    Else
    Call PasteAndDown
    End If
    End Sub
    To add code to a workbook. Hit Alt+F11. Hit Alt+I+M to insert new module. Then paste code.
    Array formulas must be entered by hitting Ctrl+Shift+Enter.

    We can't solve problems by using the same kind of thinking we used when we created them.

    Imagination is more important than knowledge.

  3. #3
    New Member
    Join Date
    Mar 2018
    Posts
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If then else not working

    Already tried that, it makes no difference

  4. #4
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    75,147
    Post Thanks / Like
    Mentioned
    61 Post(s)
    Tagged
    6 Thread(s)

    Default Re: If then else not working

    Does it work if you don't use ActiveCell/Select?
    Code:
    Sub Wednesday()
    
        If Range("graynext").Offset(-2, 0) = "W" Then
            Call PasteandcalcSat
        Else
            Call PasteAndDown
        End If
    
    End Sub
    If posting code please use code tags.

  5. #5
    New Member
    Join Date
    Mar 2018
    Posts
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If then else not working

    No, still jumps to else.
    I have similar code in Pasteanddown and PasteandcalcSat that detects if the cell two above is "S" and moves two columns to the right and that works fine.

  6. #6
    Board Regular
    Join Date
    Feb 2002
    Location
    North Yorkshire, UK
    Posts
    263
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If then else not working

    Is your value "W" or "w"? There is a difference as the code as written is case sensitive.

    Try forcing the check to always be in upper case as follows:
    Code:
    If UCase(Range("graynext").Offset(-2, 0)) = "W" Then
    It's curtains for your Dr. Horrible ... lacy, gently wafting curtains.

  7. #7
    New Member
    Join Date
    Mar 2018
    Posts
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If then else not working

    Nope, still the same

  8. #8
    New Member
    Join Date
    Mar 2018
    Posts
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If then else not working

    Oddly if I set it to detect "S" and move the active cell to 2 down from"S" it works! But I need it to work with"W"

  9. #9
    New Member
    Join Date
    Mar 2018
    Posts
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If then else not working

    Cured it, not sure exactly how and why but I changed the W to an S and it worked then I changed it back to W and it started working properly. Copying the cell to the other instances of W solves the problem. So the code was fine it was something to do with the cell format.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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