"nested If" In Vba Code

  • Hi everyone,
    I'm fine with Nested if formulas in Excel, but what do you do when a macro needs a nested if - or an "And" "Or" type argument?

    e.g. in a Loop macro, I only wanted it to run if the Country code in col A was "UK" or "IE". This is how I got round it, but is there a more elegant solution - the below would get a bit messy if there were more than 2 variables involved.

    The above works, but hopefully there's a better way to do this.
    Many thanks,

  • Re: "nested If" In Vba Code

    Hi Dave

    You essentially have two options: an If or a Case Select statement:

    The benefit of the Select Case is that if you have many criteria to include, it is much more readable and easier to debug. You can have multiple cases too.

    Hope this helps!


  • Re: "nested If" In Vba Code

    Thanks Parsnip -

    I didn't know you could combine an "If" and "Or" statement in VBA (presumably means you can do If...And as well). That will really help me a lot.

    The Select Case often doesn't meet my needs as it's normally a long macro underneath this bit and I don't want to duplicate the code if I can help it. Also, I'm often trying to combine 2 different conditions.

    If ...Or is great though - thanks again.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!