+ Reply to Thread
Results 1 to 7 of 7

IF condition not working

  1. #1
    Registered User
    Join Date
    07-10-2018
    Location
    London
    MS-Off Ver
    2013
    Posts
    11

    IF condition not working

    Good evening all,

    I am trying to run a basic IF condition with a loop. But VBA is giving some unexpected results.
    See data below, if the data columns become misaligned, then my sincere apologies.

    If Distribution channel is 99, I want Comments to say "No Door". If Distribution channel is 0, I want comments to say "Door". Please note the top left "cell" is with 0 value is cell Y3, which is the starting point for my code.


    Door Distribution Channel Comments
    0 99
    960353 0
    960353 0
    960673 0
    960673 0
    0 99
    0 99



    Please Login or Register  to view this content.

    For some strange reason, this sub routine changes the value of the distribution channel to value 0, instead of testing whether the value is 0 or 99. This is the outcome of the sub routine.

    Door Distribution Channel Comments
    0 0 Door
    960353 0 Door
    960353 0 Door
    960673 0 Door
    960673 0 Door
    0 0 Door
    0 0 Door


    Could anyone please explain why it is changing the value of the distribution channel instead of testing it? Many thanks!
    Last edited by AliGW; 07-10-2018 at 01:42 PM. Reason: Missing code tags added.

  2. #2
    Forum Contributor
    Join Date
    05-12-2018
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    266

    Re: IF condition not working

    Distchannel = ...... instead of .......... = distchannel

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: IF condition not working

    ActiveCell.Offset(0, 1).Value = DistChannel means that cell is being made the value of DistChannel which is always 0 since no value is ever given to it.

    As SjonR said, you need to reverse that since you meant to make DistChannel the value of the cell:

    DistChannel = ActiveCell.Offset(0, 1).Value
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: IF condition not working

    Quote Originally Posted by trebleo View Post
    I am trying to run a basic IF condition with a loop.
    Hi !

    Never need a loop when it's basic ! Just use Excel basics like an easy beginner formula :

    PHP Code: 
    Sub Demo1()
        
    With Range("AA3", [Z2].End(xlDown)(12))
            .
    Formula "=IF(Z3=0,""Door"",IF(Z3=99,""No Door"",""Error""))"
            
    .Formula = .Value
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: IF condition not working


    Or via a bit more advanced way :

    PHP Code: 
    Sub Demo2()
        
    With Range("Z3", [Z2].End(xlDown))
            .
    Offset(, 1).Value Evaluate(Replace("=IF(#=0,""Door"",IF(#=99,""No Door"",""Error""))""#", .Address))
        
    End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  6. #6
    Registered User
    Join Date
    07-10-2018
    Location
    London
    MS-Off Ver
    2013
    Posts
    11

    Re: IF condition not working

    Hi Sjon and Arkadi.

    Thanks for the quick responses. I was under the impression that it didnt matter whether the variable name was left or right of the = sign. Thanks for correcting this.

  7. #7
    Registered User
    Join Date
    07-10-2018
    Location
    London
    MS-Off Ver
    2013
    Posts
    11

    Re: IF condition not working

    Hi Marc L,

    Yes, I was just testing using a basic instruction to tests variables and loops. Im still a beginner with VBA, but I can make sense of your IF formula. My instructions were basic enough not to require variables or loops as you have shown. Thank you!

+ 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. SUMIFS condition not working
    By mangolah in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 03-20-2017, 11:14 AM
  2. [SOLVED] Working formulacodes in vba need run by condition
    By abjac in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-15-2015, 05:27 AM
  3. vba is not working on specific condition
    By SHEIKH TALIB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-22-2014, 12:25 PM
  4. [SOLVED] If condition not working with InStrRev
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-09-2014, 06:40 AM
  5. Join condition not working in VBA
    By cruise.alter in forum Access Tables & Databases
    Replies: 6
    Last Post: 08-01-2012, 08:12 AM
  6. If Condition not working
    By luckyros in forum Excel General
    Replies: 6
    Last Post: 12-16-2011, 05:37 AM
  7. If condition not working in the loop
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2011, 07:22 AM

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