VBA: Single line if statement with multiple actions Announcing the arrival of Valued Associate...

All ASCII characters with a given bit count

What is /etc/mtab in Linux?

Will I have to go through TSA security when I return to the US after preclearance in Atlanta?

How to translate "red flag" into Spanish?

What helicopter has the most rotor blades?

When I export an AI 300x60 art board it saves with bigger dimensions

Specify the range of GridLines

Eigenvalues of the Laplacian of the directed De Bruijn graph

How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?

What happened to Viserion in Season 7?

How to keep bees out of canned beverages?

Has a Nobel Peace laureate ever been accused of war crimes?

Translate text contents of an existing file from lower to upper case and copy to a new file

What is ls Largest Number Formed by only moving two sticks in 508?

Not within Jobscope - Aggravated injury

Why would the Overseers waste their stock of slaves on the Game?

Is there a verb for listening stealthily?

My admission is revoked after accepting the admission offer

Can gravitational waves pass through a black hole?

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

What do you call an IPA symbol that lacks a name (e.g. ɲ)?

Why did Israel vote against lifting the American embargo on Cuba?

Putting Ant-Man on house arrest

A journey... into the MIND



VBA: Single line if statement with multiple actions



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What am I missing in this code: “Else without if error”?Is there a way to crack the password on an Excel VBA Project?How to avoid using Select in Excel VBAExcel VBA Object required error on VBA Script do duplicate lines while counterExcel VBA URLDownloadToFile Error for HTTPSresourceDeleting a file in VBA - Error 53VBA : Performance on 24-deep nested IF statementVBA looping through each file in folderHow to update Sql server table from excel data using vba code?How to use multiple If Then statements in VBAVBA to check if file is open





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







6















I really should be able to google this, but I can't find what I wanna know about.



I want to check if a file exists. If not, a MessageBox should pop up and VBA should exit the sub.



If Dir("C:file.txt", vbDirectory) = "" Then 
MsgBox "File doesn't exist"
Exit Sub
End If


It works, I just wanna know if you can you do this in a single line statement? Does VBA allow this when more than one thing is supposed to happen (like it is the case here)? This code doesn't work (syntax error):



If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" And Exit Sub









share|improve this question







New contributor




NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 7





    As a personal opinion: I don't think code-readability is worth sacrificing for more compact code. So while it may be good here, I wouldn't recommend transforming your entire code into one-liners

    – Rawrplus
    9 hours ago








  • 3





    I agree with @Rawrplus - it makes it very hard to read with no gain

    – Tom
    9 hours ago






  • 1





    To further add to my comment - if you want to shorten your code for sake of readability, then don't. When you got 20k lines of codes, structurally divide your code into categories, create separate modules by logical structure and so on. But code should always be above all well comprehensive only then aesthitcally pleasing. Trust me, upon re-visiting the code, your colleagues, testers and your future self will thank you

    – Rawrplus
    9 hours ago








  • 1





    i think the keyword you are looking for is "ternary operator"

    – aaaaaa
    4 hours ago






  • 2





    I would definitely not inline an early return inside a conditional statement. An exit is something that should be made to stand out, in any programming language. Use the four-line version. Anyone reading your code will be happy you did.

    – jpmc26
    3 hours ago




















6















I really should be able to google this, but I can't find what I wanna know about.



I want to check if a file exists. If not, a MessageBox should pop up and VBA should exit the sub.



If Dir("C:file.txt", vbDirectory) = "" Then 
MsgBox "File doesn't exist"
Exit Sub
End If


It works, I just wanna know if you can you do this in a single line statement? Does VBA allow this when more than one thing is supposed to happen (like it is the case here)? This code doesn't work (syntax error):



If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" And Exit Sub









share|improve this question







New contributor




NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 7





    As a personal opinion: I don't think code-readability is worth sacrificing for more compact code. So while it may be good here, I wouldn't recommend transforming your entire code into one-liners

    – Rawrplus
    9 hours ago








  • 3





    I agree with @Rawrplus - it makes it very hard to read with no gain

    – Tom
    9 hours ago






  • 1





    To further add to my comment - if you want to shorten your code for sake of readability, then don't. When you got 20k lines of codes, structurally divide your code into categories, create separate modules by logical structure and so on. But code should always be above all well comprehensive only then aesthitcally pleasing. Trust me, upon re-visiting the code, your colleagues, testers and your future self will thank you

    – Rawrplus
    9 hours ago








  • 1





    i think the keyword you are looking for is "ternary operator"

    – aaaaaa
    4 hours ago






  • 2





    I would definitely not inline an early return inside a conditional statement. An exit is something that should be made to stand out, in any programming language. Use the four-line version. Anyone reading your code will be happy you did.

    – jpmc26
    3 hours ago
















6












6








6


1






I really should be able to google this, but I can't find what I wanna know about.



I want to check if a file exists. If not, a MessageBox should pop up and VBA should exit the sub.



If Dir("C:file.txt", vbDirectory) = "" Then 
MsgBox "File doesn't exist"
Exit Sub
End If


It works, I just wanna know if you can you do this in a single line statement? Does VBA allow this when more than one thing is supposed to happen (like it is the case here)? This code doesn't work (syntax error):



If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" And Exit Sub









share|improve this question







New contributor




NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I really should be able to google this, but I can't find what I wanna know about.



I want to check if a file exists. If not, a MessageBox should pop up and VBA should exit the sub.



If Dir("C:file.txt", vbDirectory) = "" Then 
MsgBox "File doesn't exist"
Exit Sub
End If


It works, I just wanna know if you can you do this in a single line statement? Does VBA allow this when more than one thing is supposed to happen (like it is the case here)? This code doesn't work (syntax error):



If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" And Exit Sub






excel vba






share|improve this question







New contributor




NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 10 hours ago









NoNameNo123NoNameNo123

443




443




New contributor




NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






NoNameNo123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 7





    As a personal opinion: I don't think code-readability is worth sacrificing for more compact code. So while it may be good here, I wouldn't recommend transforming your entire code into one-liners

    – Rawrplus
    9 hours ago








  • 3





    I agree with @Rawrplus - it makes it very hard to read with no gain

    – Tom
    9 hours ago






  • 1





    To further add to my comment - if you want to shorten your code for sake of readability, then don't. When you got 20k lines of codes, structurally divide your code into categories, create separate modules by logical structure and so on. But code should always be above all well comprehensive only then aesthitcally pleasing. Trust me, upon re-visiting the code, your colleagues, testers and your future self will thank you

    – Rawrplus
    9 hours ago








  • 1





    i think the keyword you are looking for is "ternary operator"

    – aaaaaa
    4 hours ago






  • 2





    I would definitely not inline an early return inside a conditional statement. An exit is something that should be made to stand out, in any programming language. Use the four-line version. Anyone reading your code will be happy you did.

    – jpmc26
    3 hours ago
















  • 7





    As a personal opinion: I don't think code-readability is worth sacrificing for more compact code. So while it may be good here, I wouldn't recommend transforming your entire code into one-liners

    – Rawrplus
    9 hours ago








  • 3





    I agree with @Rawrplus - it makes it very hard to read with no gain

    – Tom
    9 hours ago






  • 1





    To further add to my comment - if you want to shorten your code for sake of readability, then don't. When you got 20k lines of codes, structurally divide your code into categories, create separate modules by logical structure and so on. But code should always be above all well comprehensive only then aesthitcally pleasing. Trust me, upon re-visiting the code, your colleagues, testers and your future self will thank you

    – Rawrplus
    9 hours ago








  • 1





    i think the keyword you are looking for is "ternary operator"

    – aaaaaa
    4 hours ago






  • 2





    I would definitely not inline an early return inside a conditional statement. An exit is something that should be made to stand out, in any programming language. Use the four-line version. Anyone reading your code will be happy you did.

    – jpmc26
    3 hours ago










7




7





As a personal opinion: I don't think code-readability is worth sacrificing for more compact code. So while it may be good here, I wouldn't recommend transforming your entire code into one-liners

– Rawrplus
9 hours ago







As a personal opinion: I don't think code-readability is worth sacrificing for more compact code. So while it may be good here, I wouldn't recommend transforming your entire code into one-liners

– Rawrplus
9 hours ago






3




3





I agree with @Rawrplus - it makes it very hard to read with no gain

– Tom
9 hours ago





I agree with @Rawrplus - it makes it very hard to read with no gain

– Tom
9 hours ago




1




1





To further add to my comment - if you want to shorten your code for sake of readability, then don't. When you got 20k lines of codes, structurally divide your code into categories, create separate modules by logical structure and so on. But code should always be above all well comprehensive only then aesthitcally pleasing. Trust me, upon re-visiting the code, your colleagues, testers and your future self will thank you

– Rawrplus
9 hours ago







To further add to my comment - if you want to shorten your code for sake of readability, then don't. When you got 20k lines of codes, structurally divide your code into categories, create separate modules by logical structure and so on. But code should always be above all well comprehensive only then aesthitcally pleasing. Trust me, upon re-visiting the code, your colleagues, testers and your future self will thank you

– Rawrplus
9 hours ago






1




1





i think the keyword you are looking for is "ternary operator"

– aaaaaa
4 hours ago





i think the keyword you are looking for is "ternary operator"

– aaaaaa
4 hours ago




2




2





I would definitely not inline an early return inside a conditional statement. An exit is something that should be made to stand out, in any programming language. Use the four-line version. Anyone reading your code will be happy you did.

– jpmc26
3 hours ago







I would definitely not inline an early return inside a conditional statement. An exit is something that should be made to stand out, in any programming language. Use the four-line version. Anyone reading your code will be happy you did.

– jpmc26
3 hours ago














4 Answers
4






active

oldest

votes


















12














You absolutely can!



If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" : Exit Sub





share|improve this answer































    10
















    • The If statement does already support single-line syntax.

      In simple terms this means, we can either have:




      1. If {boolean-expression} Then
        {execution}
        End If


      2. If {boolean-expression} Then {execution}




        • Note the lack of End If at the second option, as it's fully omitted in single-line syntax

        • Also keep in mind, the execution block can only contain a single statement










    • Then, further way of concatenating the code together is with the : which acts as a new line in the compiler.



      This is fairly common practice in variable declaration:



      Dim x As Integer: x = 42





    Now, let's apply those steps together:





    1. The original code



      If Dir("C:file.txt", vbDirectory) = "" Then 
      MsgBox "File doesn't exist"
      Exit Sub
      End If



    2. Applying the single-line If syntax



      If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
      Exit Sub



    3. Use the : symbol to put Exit Sub into our single-line If



      If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub







    share|improve this answer





















    • 1





      This (particularly your step #2) suggests that the two forms are not equivalent (that is, the Exit Sub is not part of the branch), which I do not believe to be the case. Or, if it is the case, all of these answers are wrong because this is then not proper equivalent code to the multi-line version.

      – Lightness Races in Orbit
      7 hours ago













    • @LightnessRacesinOrbit You're right, in the sense that step #2 is not equivalent, because in that step Exit Sub is executed regardless of the if expression.

      – Louis
      6 hours ago











    • @Louis It's an interesting language feature that simulates a lexical newline and causes a logical end-of-statement in one sense, without causing a logical end-of-statement in another! VB's blocks are weird.

      – Lightness Races in Orbit
      6 hours ago













    • @LightnessRacesinOrbit Yes. It's technically 2 lines merged artificially into one with :. Really it's about semantics of what you consider a single-line expression. It's a cosmetically merged expression (:) consisting of two single-line expression. The : serves no practical purpose, other than visually fusing two new-lines

      – Rawrplus
      6 hours ago













    • @Rawrplus But it's slightly more than that, because without the :, the two substatements would in fact be one [invalid] substatement.

      – Lightness Races in Orbit
      6 hours ago



















    3














    In VBA you can execute even more than two lines of code in one, just add : between one instruction and the other! This is perfectly legal:



    If True Then MsgBox "True - Line 1": MsgBox "True - Line 2": Exit Sub





    share|improve this answer

































      1














      If Dir("C:file.txt", vbDirectory) = "" Then : MsgBox "File doesn't exist" : Exit Sub


      I do not have enough reputation to fix the answer above. : should be added between Then and your action block as well.






      share|improve this answer



















      • 6





        Actually There's no need to put : after Then. The @Zamar answer is correct.

        – Louis
        9 hours ago











      • I am getting an error, though. 'End If' must be preceded by a matching 'If'

        – wEight
        9 hours ago













      • It must be for another reason, I just copied and executed his code and it works (so does mine)...

        – Louis
        9 hours ago











      • Extraordinary...

        – wEight
        9 hours ago











      • @wEight but there's no End If in your code

        – Winand
        4 hours ago












      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      NoNameNo123 is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55811381%2fvba-single-line-if-statement-with-multiple-actions%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      12














      You absolutely can!



      If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" : Exit Sub





      share|improve this answer




























        12














        You absolutely can!



        If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" : Exit Sub





        share|improve this answer


























          12












          12








          12







          You absolutely can!



          If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" : Exit Sub





          share|improve this answer













          You absolutely can!



          If Dir("C:file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" : Exit Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 10 hours ago









          ZamarZamar

          169110




          169110

























              10
















              • The If statement does already support single-line syntax.

                In simple terms this means, we can either have:




                1. If {boolean-expression} Then
                  {execution}
                  End If


                2. If {boolean-expression} Then {execution}




                  • Note the lack of End If at the second option, as it's fully omitted in single-line syntax

                  • Also keep in mind, the execution block can only contain a single statement










              • Then, further way of concatenating the code together is with the : which acts as a new line in the compiler.



                This is fairly common practice in variable declaration:



                Dim x As Integer: x = 42





              Now, let's apply those steps together:





              1. The original code



                If Dir("C:file.txt", vbDirectory) = "" Then 
                MsgBox "File doesn't exist"
                Exit Sub
                End If



              2. Applying the single-line If syntax



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
                Exit Sub



              3. Use the : symbol to put Exit Sub into our single-line If



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub







              share|improve this answer





















              • 1





                This (particularly your step #2) suggests that the two forms are not equivalent (that is, the Exit Sub is not part of the branch), which I do not believe to be the case. Or, if it is the case, all of these answers are wrong because this is then not proper equivalent code to the multi-line version.

                – Lightness Races in Orbit
                7 hours ago













              • @LightnessRacesinOrbit You're right, in the sense that step #2 is not equivalent, because in that step Exit Sub is executed regardless of the if expression.

                – Louis
                6 hours ago











              • @Louis It's an interesting language feature that simulates a lexical newline and causes a logical end-of-statement in one sense, without causing a logical end-of-statement in another! VB's blocks are weird.

                – Lightness Races in Orbit
                6 hours ago













              • @LightnessRacesinOrbit Yes. It's technically 2 lines merged artificially into one with :. Really it's about semantics of what you consider a single-line expression. It's a cosmetically merged expression (:) consisting of two single-line expression. The : serves no practical purpose, other than visually fusing two new-lines

                – Rawrplus
                6 hours ago













              • @Rawrplus But it's slightly more than that, because without the :, the two substatements would in fact be one [invalid] substatement.

                – Lightness Races in Orbit
                6 hours ago
















              10
















              • The If statement does already support single-line syntax.

                In simple terms this means, we can either have:




                1. If {boolean-expression} Then
                  {execution}
                  End If


                2. If {boolean-expression} Then {execution}




                  • Note the lack of End If at the second option, as it's fully omitted in single-line syntax

                  • Also keep in mind, the execution block can only contain a single statement










              • Then, further way of concatenating the code together is with the : which acts as a new line in the compiler.



                This is fairly common practice in variable declaration:



                Dim x As Integer: x = 42





              Now, let's apply those steps together:





              1. The original code



                If Dir("C:file.txt", vbDirectory) = "" Then 
                MsgBox "File doesn't exist"
                Exit Sub
                End If



              2. Applying the single-line If syntax



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
                Exit Sub



              3. Use the : symbol to put Exit Sub into our single-line If



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub







              share|improve this answer





















              • 1





                This (particularly your step #2) suggests that the two forms are not equivalent (that is, the Exit Sub is not part of the branch), which I do not believe to be the case. Or, if it is the case, all of these answers are wrong because this is then not proper equivalent code to the multi-line version.

                – Lightness Races in Orbit
                7 hours ago













              • @LightnessRacesinOrbit You're right, in the sense that step #2 is not equivalent, because in that step Exit Sub is executed regardless of the if expression.

                – Louis
                6 hours ago











              • @Louis It's an interesting language feature that simulates a lexical newline and causes a logical end-of-statement in one sense, without causing a logical end-of-statement in another! VB's blocks are weird.

                – Lightness Races in Orbit
                6 hours ago













              • @LightnessRacesinOrbit Yes. It's technically 2 lines merged artificially into one with :. Really it's about semantics of what you consider a single-line expression. It's a cosmetically merged expression (:) consisting of two single-line expression. The : serves no practical purpose, other than visually fusing two new-lines

                – Rawrplus
                6 hours ago













              • @Rawrplus But it's slightly more than that, because without the :, the two substatements would in fact be one [invalid] substatement.

                – Lightness Races in Orbit
                6 hours ago














              10












              10








              10









              • The If statement does already support single-line syntax.

                In simple terms this means, we can either have:




                1. If {boolean-expression} Then
                  {execution}
                  End If


                2. If {boolean-expression} Then {execution}




                  • Note the lack of End If at the second option, as it's fully omitted in single-line syntax

                  • Also keep in mind, the execution block can only contain a single statement










              • Then, further way of concatenating the code together is with the : which acts as a new line in the compiler.



                This is fairly common practice in variable declaration:



                Dim x As Integer: x = 42





              Now, let's apply those steps together:





              1. The original code



                If Dir("C:file.txt", vbDirectory) = "" Then 
                MsgBox "File doesn't exist"
                Exit Sub
                End If



              2. Applying the single-line If syntax



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
                Exit Sub



              3. Use the : symbol to put Exit Sub into our single-line If



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub







              share|improve this answer

















              • The If statement does already support single-line syntax.

                In simple terms this means, we can either have:




                1. If {boolean-expression} Then
                  {execution}
                  End If


                2. If {boolean-expression} Then {execution}




                  • Note the lack of End If at the second option, as it's fully omitted in single-line syntax

                  • Also keep in mind, the execution block can only contain a single statement










              • Then, further way of concatenating the code together is with the : which acts as a new line in the compiler.



                This is fairly common practice in variable declaration:



                Dim x As Integer: x = 42





              Now, let's apply those steps together:





              1. The original code



                If Dir("C:file.txt", vbDirectory) = "" Then 
                MsgBox "File doesn't exist"
                Exit Sub
                End If



              2. Applying the single-line If syntax



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
                Exit Sub



              3. Use the : symbol to put Exit Sub into our single-line If



                If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 3 hours ago

























              answered 9 hours ago









              RawrplusRawrplus

              2,82541331




              2,82541331








              • 1





                This (particularly your step #2) suggests that the two forms are not equivalent (that is, the Exit Sub is not part of the branch), which I do not believe to be the case. Or, if it is the case, all of these answers are wrong because this is then not proper equivalent code to the multi-line version.

                – Lightness Races in Orbit
                7 hours ago













              • @LightnessRacesinOrbit You're right, in the sense that step #2 is not equivalent, because in that step Exit Sub is executed regardless of the if expression.

                – Louis
                6 hours ago











              • @Louis It's an interesting language feature that simulates a lexical newline and causes a logical end-of-statement in one sense, without causing a logical end-of-statement in another! VB's blocks are weird.

                – Lightness Races in Orbit
                6 hours ago













              • @LightnessRacesinOrbit Yes. It's technically 2 lines merged artificially into one with :. Really it's about semantics of what you consider a single-line expression. It's a cosmetically merged expression (:) consisting of two single-line expression. The : serves no practical purpose, other than visually fusing two new-lines

                – Rawrplus
                6 hours ago













              • @Rawrplus But it's slightly more than that, because without the :, the two substatements would in fact be one [invalid] substatement.

                – Lightness Races in Orbit
                6 hours ago














              • 1





                This (particularly your step #2) suggests that the two forms are not equivalent (that is, the Exit Sub is not part of the branch), which I do not believe to be the case. Or, if it is the case, all of these answers are wrong because this is then not proper equivalent code to the multi-line version.

                – Lightness Races in Orbit
                7 hours ago













              • @LightnessRacesinOrbit You're right, in the sense that step #2 is not equivalent, because in that step Exit Sub is executed regardless of the if expression.

                – Louis
                6 hours ago











              • @Louis It's an interesting language feature that simulates a lexical newline and causes a logical end-of-statement in one sense, without causing a logical end-of-statement in another! VB's blocks are weird.

                – Lightness Races in Orbit
                6 hours ago













              • @LightnessRacesinOrbit Yes. It's technically 2 lines merged artificially into one with :. Really it's about semantics of what you consider a single-line expression. It's a cosmetically merged expression (:) consisting of two single-line expression. The : serves no practical purpose, other than visually fusing two new-lines

                – Rawrplus
                6 hours ago













              • @Rawrplus But it's slightly more than that, because without the :, the two substatements would in fact be one [invalid] substatement.

                – Lightness Races in Orbit
                6 hours ago








              1




              1





              This (particularly your step #2) suggests that the two forms are not equivalent (that is, the Exit Sub is not part of the branch), which I do not believe to be the case. Or, if it is the case, all of these answers are wrong because this is then not proper equivalent code to the multi-line version.

              – Lightness Races in Orbit
              7 hours ago







              This (particularly your step #2) suggests that the two forms are not equivalent (that is, the Exit Sub is not part of the branch), which I do not believe to be the case. Or, if it is the case, all of these answers are wrong because this is then not proper equivalent code to the multi-line version.

              – Lightness Races in Orbit
              7 hours ago















              @LightnessRacesinOrbit You're right, in the sense that step #2 is not equivalent, because in that step Exit Sub is executed regardless of the if expression.

              – Louis
              6 hours ago





              @LightnessRacesinOrbit You're right, in the sense that step #2 is not equivalent, because in that step Exit Sub is executed regardless of the if expression.

              – Louis
              6 hours ago













              @Louis It's an interesting language feature that simulates a lexical newline and causes a logical end-of-statement in one sense, without causing a logical end-of-statement in another! VB's blocks are weird.

              – Lightness Races in Orbit
              6 hours ago







              @Louis It's an interesting language feature that simulates a lexical newline and causes a logical end-of-statement in one sense, without causing a logical end-of-statement in another! VB's blocks are weird.

              – Lightness Races in Orbit
              6 hours ago















              @LightnessRacesinOrbit Yes. It's technically 2 lines merged artificially into one with :. Really it's about semantics of what you consider a single-line expression. It's a cosmetically merged expression (:) consisting of two single-line expression. The : serves no practical purpose, other than visually fusing two new-lines

              – Rawrplus
              6 hours ago







              @LightnessRacesinOrbit Yes. It's technically 2 lines merged artificially into one with :. Really it's about semantics of what you consider a single-line expression. It's a cosmetically merged expression (:) consisting of two single-line expression. The : serves no practical purpose, other than visually fusing two new-lines

              – Rawrplus
              6 hours ago















              @Rawrplus But it's slightly more than that, because without the :, the two substatements would in fact be one [invalid] substatement.

              – Lightness Races in Orbit
              6 hours ago





              @Rawrplus But it's slightly more than that, because without the :, the two substatements would in fact be one [invalid] substatement.

              – Lightness Races in Orbit
              6 hours ago











              3














              In VBA you can execute even more than two lines of code in one, just add : between one instruction and the other! This is perfectly legal:



              If True Then MsgBox "True - Line 1": MsgBox "True - Line 2": Exit Sub





              share|improve this answer






























                3














                In VBA you can execute even more than two lines of code in one, just add : between one instruction and the other! This is perfectly legal:



                If True Then MsgBox "True - Line 1": MsgBox "True - Line 2": Exit Sub





                share|improve this answer




























                  3












                  3








                  3







                  In VBA you can execute even more than two lines of code in one, just add : between one instruction and the other! This is perfectly legal:



                  If True Then MsgBox "True - Line 1": MsgBox "True - Line 2": Exit Sub





                  share|improve this answer















                  In VBA you can execute even more than two lines of code in one, just add : between one instruction and the other! This is perfectly legal:



                  If True Then MsgBox "True - Line 1": MsgBox "True - Line 2": Exit Sub






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 6 hours ago

























                  answered 9 hours ago









                  LouisLouis

                  1,018412




                  1,018412























                      1














                      If Dir("C:file.txt", vbDirectory) = "" Then : MsgBox "File doesn't exist" : Exit Sub


                      I do not have enough reputation to fix the answer above. : should be added between Then and your action block as well.






                      share|improve this answer



















                      • 6





                        Actually There's no need to put : after Then. The @Zamar answer is correct.

                        – Louis
                        9 hours ago











                      • I am getting an error, though. 'End If' must be preceded by a matching 'If'

                        – wEight
                        9 hours ago













                      • It must be for another reason, I just copied and executed his code and it works (so does mine)...

                        – Louis
                        9 hours ago











                      • Extraordinary...

                        – wEight
                        9 hours ago











                      • @wEight but there's no End If in your code

                        – Winand
                        4 hours ago
















                      1














                      If Dir("C:file.txt", vbDirectory) = "" Then : MsgBox "File doesn't exist" : Exit Sub


                      I do not have enough reputation to fix the answer above. : should be added between Then and your action block as well.






                      share|improve this answer



















                      • 6





                        Actually There's no need to put : after Then. The @Zamar answer is correct.

                        – Louis
                        9 hours ago











                      • I am getting an error, though. 'End If' must be preceded by a matching 'If'

                        – wEight
                        9 hours ago













                      • It must be for another reason, I just copied and executed his code and it works (so does mine)...

                        – Louis
                        9 hours ago











                      • Extraordinary...

                        – wEight
                        9 hours ago











                      • @wEight but there's no End If in your code

                        – Winand
                        4 hours ago














                      1












                      1








                      1







                      If Dir("C:file.txt", vbDirectory) = "" Then : MsgBox "File doesn't exist" : Exit Sub


                      I do not have enough reputation to fix the answer above. : should be added between Then and your action block as well.






                      share|improve this answer













                      If Dir("C:file.txt", vbDirectory) = "" Then : MsgBox "File doesn't exist" : Exit Sub


                      I do not have enough reputation to fix the answer above. : should be added between Then and your action block as well.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 9 hours ago









                      wEightwEight

                      356




                      356








                      • 6





                        Actually There's no need to put : after Then. The @Zamar answer is correct.

                        – Louis
                        9 hours ago











                      • I am getting an error, though. 'End If' must be preceded by a matching 'If'

                        – wEight
                        9 hours ago













                      • It must be for another reason, I just copied and executed his code and it works (so does mine)...

                        – Louis
                        9 hours ago











                      • Extraordinary...

                        – wEight
                        9 hours ago











                      • @wEight but there's no End If in your code

                        – Winand
                        4 hours ago














                      • 6





                        Actually There's no need to put : after Then. The @Zamar answer is correct.

                        – Louis
                        9 hours ago











                      • I am getting an error, though. 'End If' must be preceded by a matching 'If'

                        – wEight
                        9 hours ago













                      • It must be for another reason, I just copied and executed his code and it works (so does mine)...

                        – Louis
                        9 hours ago











                      • Extraordinary...

                        – wEight
                        9 hours ago











                      • @wEight but there's no End If in your code

                        – Winand
                        4 hours ago








                      6




                      6





                      Actually There's no need to put : after Then. The @Zamar answer is correct.

                      – Louis
                      9 hours ago





                      Actually There's no need to put : after Then. The @Zamar answer is correct.

                      – Louis
                      9 hours ago













                      I am getting an error, though. 'End If' must be preceded by a matching 'If'

                      – wEight
                      9 hours ago







                      I am getting an error, though. 'End If' must be preceded by a matching 'If'

                      – wEight
                      9 hours ago















                      It must be for another reason, I just copied and executed his code and it works (so does mine)...

                      – Louis
                      9 hours ago





                      It must be for another reason, I just copied and executed his code and it works (so does mine)...

                      – Louis
                      9 hours ago













                      Extraordinary...

                      – wEight
                      9 hours ago





                      Extraordinary...

                      – wEight
                      9 hours ago













                      @wEight but there's no End If in your code

                      – Winand
                      4 hours ago





                      @wEight but there's no End If in your code

                      – Winand
                      4 hours ago










                      NoNameNo123 is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      NoNameNo123 is a new contributor. Be nice, and check out our Code of Conduct.













                      NoNameNo123 is a new contributor. Be nice, and check out our Code of Conduct.












                      NoNameNo123 is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55811381%2fvba-single-line-if-statement-with-multiple-actions%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown