+ Reply to Thread
Results 1 to 8 of 8

Conditional Statements & Nested If/Then in VBA

  1. #1
    Registered User
    Join Date
    02-09-2018
    Location
    US
    MS-Off Ver
    2016
    Posts
    5

    Unhappy Conditional Statements & Nested If/Then in VBA

    I am trying to write a function or sub which passes in 3 variables from the worksheet and returns a number 0-3 based on conditional statements (nested If/then). However, the func returns ‘0’ for all cases. Using breaks in the module, I can see this is because, while the variables have been passed in (UpIso, Con, DIso), the function (isolation) =’empty’

    My code is below. Any help is appreciated. Thanks

    Please Login or Register  to view this content.
    Last edited by 6StringJazzer; 02-09-2018 at 10:46 AM. Reason: added CODE tags

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

    Re: Conditional Statements & Nested If/Then in VBA

    I strongly recommend to everyone that they declare variables. Doing so prevents a lot of bugs and runtime errors.

    If you do this you will find that you have variables Y, N, FD, Station, and NA which are undeclared, and also unset. Therefore they are all 0. I can't tell for sure without seeing your file but I assume you are looking for character strings.

    Please Login or Register  to view this content.
    Also, when you post code, use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    02-09-2018
    Location
    US
    MS-Off Ver
    2016
    Posts
    5

    Re: Conditional Statements & Nested If/Then in VBA

    Thanks for comments on general formatting, I'll keep that in mind. I've declared the variables from the worksheet as variant, but still have the same problem. Here is the new code:


    Please Login or Register  to view this content.
    Last edited by jjjhs12; 02-09-2018 at 09:47 AM.

  4. #4
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Conditional Statements & Nested If/Then in VBA

    What happens when you step through the code? does it drop out anywhere or does it perform as you would expect but still give the wrong answer?
    If someone has helped you then please add to their Reputation

  5. #5
    Registered User
    Join Date
    02-09-2018
    Location
    US
    MS-Off Ver
    2016
    Posts
    5

    Re: Conditional Statements & Nested If/Then in VBA

    pjwhitfield, good question.

    It runs the first if/then statement as 'false' returning 'isolation=empty'. Therefore, it doesn't run any of the following ElseIf statements.

  6. #6
    Registered User
    Join Date
    02-09-2018
    Location
    US
    MS-Off Ver
    2016
    Posts
    5

    Re: Conditional Statements & Nested If/Then in VBA

    Perhaps I need to add a Do loop?

  7. #7
    Registered User
    Join Date
    02-09-2018
    Location
    US
    MS-Off Ver
    2016
    Posts
    5

    Re: Conditional Statements & Nested If/Then in VBA

    pjwhitfield, here's a look at that

    vba .jpg

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

    Re: Conditional Statements & Nested If/Then in VBA

    Quote Originally Posted by jjjhs12 View Post
    I've declared the variables from the worksheet as variant, but still have the same problem.
    You missed my point. Those values need to be in quotes, not variables. I provided the code. You seem to have ignored it.

    I suggested Option Explicit because if you had used it, you would have gotten compiler errors and realized you needed quotes instead of variables.
    Last edited by 6StringJazzer; 02-09-2018 at 10:47 AM.

+ 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. [SOLVED] Nested if statements containing multiple and statements
    By John M. in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-22-2014, 11:48 PM
  2. questions with if statements and nested if statements
    By Pat Excel in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 02-03-2013, 01:41 PM
  3. Nested if statements with AND and OR statements
    By joeljoel in forum Excel General
    Replies: 4
    Last Post: 08-08-2011, 12:57 PM
  4. Multiple nested IF statements and AND statements
    By TonyGetz in forum Excel General
    Replies: 2
    Last Post: 12-14-2010, 03:07 AM
  5. Nested If statements / logic statements
    By Brainless_09 in forum Excel General
    Replies: 3
    Last Post: 06-17-2009, 02:59 PM
  6. Replies: 12
    Last Post: 05-15-2009, 08:38 AM
  7. Nested conditional statements
    By Apel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2006, 01:28 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