dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] [VBA] IF does not fail

  1. #1
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    [RESOLVED] [VBA] IF does not fail

    I've got this if statement
    Code:
    If valueType = "int" And IsNumeric(value) And CLng(value) Then
    ...
    End If
    the problem is, when the "value" is not numeric (this means the function IsNumeric returns False) the last test is being performed anyway and I get the type mismatch error. why is this happening and how can I fix it?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: [VBA] IF does not fail

    Why don't you use nested if..then instead?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: [VBA] IF does not fail

    because it's a part of many tests and it would impact the readability... it's a very strange behavior, from c# or c++ I'm used to something else, the third test would never be called if the second one returned false.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: [VBA] IF does not fail

    When checking for variable type, you should never use that in conjunction with other if statements. That should be checked before proceeding further with the variable.

    If you want code to be "readable" then you need to use commenting. That is why commenting exists. So that humans can clearly understand their thought processes as they tell computers what to do.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: [VBA] IF does not fail

    I just cannot agree with what you're saying, it's against everything I've learnt so far
    Quote Originally Posted by PeejAvery View Post
    When checking for variable type, you should never use that in conjunction with other if statements. That should be checked before proceeding further with the variable.
    this is excaly what I'm doing: and the result is, TRUE, FALSE, TYPE MISMATCH... but it should be: TRUE, FALSE, ?? ... and at the FALSE it should go further to the next statement and not evaluating the third test because it will throw an exception for sure

    Quote Originally Posted by PeejAvery View Post
    If you want code to be "readable" then you need to use commenting. That is why commenting exists. So that humans can clearly understand their thought processes as they tell computers what to do.
    commenting is not everything and deeply nested ifs are still worse
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: [VBA] IF does not fail

    VB does not short circuit the ANDs in you IF as it would in C#, C++ and Java and so on.... So I would imagine that VBA doesn't either.
    That means you'll evaluate ALL expressions in your IF. You have to nest the IFs instead.

    I know in VB.NET there is the "AndAlso" (silly name, but it works ... and the similar OrElse) which does short circuit, but I'm unsure (actually I doubt it would cause of age) whether it exists in VBA as well as it is a newcomer in the .NET version of VB.
    Last edited by Alsvha; July 23rd, 2009 at 12:54 AM.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: [VBA] IF does not fail

    Good calls Alsvha and Peej.

    I would rewrite the statement like this :
    PHP Code:
    If valueType "int" Then
        
    If IsNumeric(valueThen
            
    If CLng(valueThen
                
    'Code To BE Executed When All Three Conditions Exist
            End If
        End If
    End If 
    AFAIK, VBA doesn't have AndAlso / OrElse, as it is only a subset of VB
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  8. #8
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: [VBA] IF does not fail

    thx for your replays, I didn't know that vba does not support this so called AndAlso (that I'v just learnt here), visual basic seems to be not so basic at all ;]

    I have rewritten my code and it works fine now even if it looks a little bit strange to me
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)