0

see the code below.

IF Weight=0 Then CallFunction

IF Weight=100 Then
Heavy=True
Else 
Heavy= False
End If

How is the program working when I have not ended a if statement?

Tarik
  • 10,810
  • 2
  • 26
  • 40
Meesha
  • 801
  • 1
  • 9
  • 27
  • Have you tried to execute it? – Maciej Los Apr 24 '15 at 20:35
  • 1
    @MaciejLos yes I have executed it and it works. – Meesha Apr 24 '15 at 20:37
  • And what's your issue? If `Weight` is equal to zero, then `CallFunction` is executed, then code is going to `if` statement. That's all! Debug the program to find out ;) – Maciej Los Apr 24 '15 at 20:40
  • I raised the question to just get expertise on how can the code run as I have been getting errors in other programs where I didn't write a End IF statement. – Meesha Apr 24 '15 at 20:43

2 Answers2

1

its VBA's functionality to end up if statements in a single line..

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • 3
    It's a language feature of the Basic language that is decades old and is present in all versions of the language. The accepted answer in the linked question concerns the `IF` operator that is a completely different thing (a ternary operator, new to VB.NET). VBA does not have the `IF` ternary operator at all, only the `IF` clause and the `IIf` function. – GSerg Apr 24 '15 at 20:45
  • 1
    @Vijay's answer _is_ correct, but as GSerg pointed out, the link isn't relevant. Maybe Vijay can edit his answer. – GuitarPicker Apr 24 '15 at 20:50
  • Newbie on stackoverflow... so needed to reply up fast to get some fair reputation.... didn't went through the answer thoroughly. thanks @GuitarPicker – Vijay Goswami Apr 24 '15 at 20:52
  • 1
    @GSerg http://stackoverflow.com/questions/20541024/if-statements-in-vba This is proper. – Meesha Apr 24 '15 at 20:53
  • 1
    @Meesha Yes it is, which is why I have voted to close as duplicate. – GSerg Apr 24 '15 at 20:57
-2

If Weight is equal to zero, then CallFunction is executed (if Weight is not equal to zero, a CallFunction is not executed), then the code is going to another if statement. That's all! Debug the program to find out ;)

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
  • The OP was wondering why it was syntactically possible to have an `If` that does not have a corresponding `End If`. – GSerg Apr 24 '15 at 20:50
  • Thank you all downvoters! No comments, no chance to improve my answer. Thank you again. – Maciej Los Apr 24 '15 at 20:50
  • No, @GSerg, OP wants to explain what code do... See: "*How is the program working when I have not ended a if statement?*" – Maciej Los Apr 24 '15 at 20:51
  • No, the OP is asking why the program is working even though they did not provide an `End If`, provided that previously, when they had forgotten to provide an `End If`, the program [would have not worked](http://stackoverflow.com/questions/29856872/if-without-end-if-in-vba/29857124?noredirect=1#comment47840102_29856872), but this time it works for some reason. "How is this working" means "Why is it working" as well in English. – GSerg Apr 24 '15 at 20:53
  • @GSerg, :laugh: `How` is not equal to `Why`! And it does not mean "Why is it working" as per my understanding. Does it is the reason of downovote? Please, read OP comments to the question. – Maciej Los Apr 24 '15 at 20:56