0

This is less of a problem question, and more of an understanding question.

I stole the code below:

Dim fd as Office.FileDialog

FileDialog.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
        .AllowMultiSelect = False
        .Title = "Please select the file to process."
        .Filters.Clear
        .Filters.Add "Text files", "*.txt"
        .InitialFileName = "C:\"
    End With
If fd.Show = -1 Then
sFilename = fd.SelectedItems(1)
Else
    MsgBox ("You did not pick a file! Cancelling macro.")
    Exit Sub
End If

I do not recall where I got the code from, but it was a while ago. I found this in an old macro and upon looking at it, I do not understand the If statement.

If fd.Show = -1 Then

I thought Application.FileDialog.Show was a boolean expression, and I understand that 0 typically means False, and 1 typically means True. What in the world does -1 mean?

The code works perfectly, and I've tried to research the topic, but everything I find says either "True" or "False". Any insight is much appreciated.

Community
  • 1
  • 1
SalvadorVayshun
  • 343
  • 1
  • 3
  • 19
  • 2
    In VB Land int(true) is -1 not 1 / See http://stackoverflow.com/questions/14462272/why-is-true-equal-to-1 – Alex K. Dec 08 '16 at 16:39
  • For the reason *why* true is -1, [see here](http://stackoverflow.com/documentation/vba/3418/data-types-and-limits/11778/boolean#t=201612081644123119612). – Comintern Dec 08 '16 at 16:45

2 Answers2

5

This would be more readable, and completely equivalent:

If fd.Show Then
    sFilename = fd.SelectedItems(1)
Else
    MsgBox "You did not pick a file! Cancelling macro."
    Exit Sub
End If

I've removed the extraneous parentheses around the MsgBox arguments, since MsgBox is being called as a procedure here and not a function, and thus all the parentheses do is force VBA to evaluate the string as a value, ...which it already is. Using extraneous parentheses as a habit can lead to stupid and unexpected bugs and compilation errors.

As already mentioned in comments, the -1 literal really just stands for True in this case.

Same as this:

If fd.Show = True Then

But then, that would be redundant, since a Boolean value is already a Boolean expression, so there's never a need to compare a Boolean value to make a Boolean expression and satisfy the If syntax:

If {bool-expression} Then
    {statements}
End If

I understand that 0 typically means False, and 1 typically means True

Not quite. 0 means False, yes, but True is just nothing more than not false: any non-zero integer value will convert to True:

Debug.Print CBool(42) 'prints True
Debug.Print CBool(-12) 'prints True
Debug.Print CBool(0) 'prints False
Graham
  • 7,431
  • 18
  • 59
  • 84
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • +1 Wow. Very nice explanation. I will have to try out the whole `Not False` idea. I've never actually tried to put anything in a boolean expression other than True and False. Thank you for the info! – SalvadorVayshun Dec 08 '16 at 17:44
  • If you'd rather avoid implicit conversion, you can do `If CBool(fs.Show) Then` for ultimate readability =) – Mathieu Guindon Dec 08 '16 at 17:47
  • Ah yes, because doing an `If` followed by a boolean is basically just stating `If bool = True`? Or do I have to explicitly use `CBool()` in order to use that? – SalvadorVayshun Dec 08 '16 at 17:51
  • @SalvadorVayshun This video explains why False is 0 and True is <> 0: [Excel Magic Trick 652: Boolean Logic for Logical & Array Formulas](https://www.youtube.com/watch?v=6XX7UBmP3rM) –  Dec 08 '16 at 17:52
  • @SalvadorVayshun Exactly - that's specifically what I meant with *that would be redundant, since a Boolean value is already a Boolean expression*. If you don't make the explicit conversion, VBA will make an implicit one. – Mathieu Guindon Dec 08 '16 at 17:52
2

Actually this Is an Office control and "Show" returns a long. -1 Means the User pressed the Accept Button and 0 Means they pressed the Cancel button.

https://msdn.microsoft.com/en-us/library/office/ff865217.aspx

Scott Dobbins
  • 294
  • 1
  • 8