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;
}
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
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.
|
show 5 more comments
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
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
|
show 5 more comments
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
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
excel vba
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.
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
|
show 5 more comments
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
|
show 5 more comments
4 Answers
4
active
oldest
votes
You absolutely can!
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File doesn't exist" : Exit Sub
add a comment |
The
Ifstatement does already support single-line syntax.
In simple terms this means, we can either have:
If {boolean-expression} Then
{execution}
End If
If {boolean-expression} Then {execution}
Note the lack ofEnd Ifat 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:
The original code
If Dir("C:file.txt", vbDirectory) = "" Then
MsgBox "File doesn't exist"
Exit Sub
End If
Applying the single-line
Ifsyntax
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
Exit Sub
Use the
:symbol to putExit Subinto our single-lineIf
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub
1
This (particularly your step #2) suggests that the two forms are not equivalent (that is, theExit Subis 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 thatstep #2is not equivalent, because in that stepExit Subis 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
|
show 2 more comments
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
add a comment |
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.
6
Actually There's no need to put:afterThen. 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 noEnd Ifin your code
– Winand
4 hours ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You absolutely can!
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File doesn't exist" : Exit Sub
add a comment |
You absolutely can!
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File doesn't exist" : Exit Sub
add a comment |
You absolutely can!
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File doesn't exist" : Exit Sub
You absolutely can!
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File doesn't exist" : Exit Sub
answered 10 hours ago
ZamarZamar
169110
169110
add a comment |
add a comment |
The
Ifstatement does already support single-line syntax.
In simple terms this means, we can either have:
If {boolean-expression} Then
{execution}
End If
If {boolean-expression} Then {execution}
Note the lack ofEnd Ifat 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:
The original code
If Dir("C:file.txt", vbDirectory) = "" Then
MsgBox "File doesn't exist"
Exit Sub
End If
Applying the single-line
Ifsyntax
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
Exit Sub
Use the
:symbol to putExit Subinto our single-lineIf
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub
1
This (particularly your step #2) suggests that the two forms are not equivalent (that is, theExit Subis 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 thatstep #2is not equivalent, because in that stepExit Subis 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
|
show 2 more comments
The
Ifstatement does already support single-line syntax.
In simple terms this means, we can either have:
If {boolean-expression} Then
{execution}
End If
If {boolean-expression} Then {execution}
Note the lack ofEnd Ifat 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:
The original code
If Dir("C:file.txt", vbDirectory) = "" Then
MsgBox "File doesn't exist"
Exit Sub
End If
Applying the single-line
Ifsyntax
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
Exit Sub
Use the
:symbol to putExit Subinto our single-lineIf
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub
1
This (particularly your step #2) suggests that the two forms are not equivalent (that is, theExit Subis 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 thatstep #2is not equivalent, because in that stepExit Subis 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
|
show 2 more comments
The
Ifstatement does already support single-line syntax.
In simple terms this means, we can either have:
If {boolean-expression} Then
{execution}
End If
If {boolean-expression} Then {execution}
Note the lack ofEnd Ifat 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:
The original code
If Dir("C:file.txt", vbDirectory) = "" Then
MsgBox "File doesn't exist"
Exit Sub
End If
Applying the single-line
Ifsyntax
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
Exit Sub
Use the
:symbol to putExit Subinto our single-lineIf
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub
The
Ifstatement does already support single-line syntax.
In simple terms this means, we can either have:
If {boolean-expression} Then
{execution}
End If
If {boolean-expression} Then {execution}
Note the lack ofEnd Ifat 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:
The original code
If Dir("C:file.txt", vbDirectory) = "" Then
MsgBox "File doesn't exist"
Exit Sub
End If
Applying the single-line
Ifsyntax
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist"
Exit Sub
Use the
:symbol to putExit Subinto our single-lineIf
If Dir("C:file.txt", vbDirectory) = "" Then MsgBox "File Doesn't Exist" : Exit Sub
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, theExit Subis 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 thatstep #2is not equivalent, because in that stepExit Subis 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
|
show 2 more comments
1
This (particularly your step #2) suggests that the two forms are not equivalent (that is, theExit Subis 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 thatstep #2is not equivalent, because in that stepExit Subis 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
|
show 2 more comments
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
add a comment |
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
add a comment |
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
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
edited 6 hours ago
answered 9 hours ago
LouisLouis
1,018412
1,018412
add a comment |
add a comment |
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.
6
Actually There's no need to put:afterThen. 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 noEnd Ifin your code
– Winand
4 hours ago
add a comment |
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.
6
Actually There's no need to put:afterThen. 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 noEnd Ifin your code
– Winand
4 hours ago
add a comment |
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.
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.
answered 9 hours ago
wEightwEight
356
356
6
Actually There's no need to put:afterThen. 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 noEnd Ifin your code
– Winand
4 hours ago
add a comment |
6
Actually There's no need to put:afterThen. 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 noEnd Ifin 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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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