title: Selected 0 files but VBA Excel says I selected 1 file
body: <p>I tried to run this script where I want a message box to state the number of open files I have selected. However, when I run the script iin VBA and click the cancel button in the file picker without having selected any files, the output of the message box states "You have selected 1 files" despite me having selected none. Could someone please help me with this?</p>
  
  <pre><code>Public OpenFiles As Variant
  Public FileNumber As Integer
  Public TextFile As Workbook
  Public FileNumberString As String
  
  Public Sub OpenFile()
  
      OpenFiles = Application.GetOpenFilename(Title:="Select File(s) to Import", MultiSelect:=True)
      FileNumber = Application.Count(OpenFiles)
      FileNumberString = FileNumber
      MsgBox ("You have selected " + FileNumberString + " Files")
  End Sub
  </code></pre>
  
  <p>I am also scripting another property which only runs if FileNumber != 0. Hence, this is a problem for me:</p>
  
  <pre><code>Public Sub ImportTextFiles()
  
      Dim i As Integer
      Application.ScreenUpdating = False
  
      If FileNumber = 0 Then
          MsgBox ("You did not select a file. Please select at least one file")
  
      Else:
          For i = 1 To FileNumber
              Set TextFile = Workbooks.Open(OpenFiles(i))
              TextFile.Sheets(1).Range("A1").CurrentRegion.Copy
              Workbooks(1).Activate
              Workbooks(1).Worksheets.Add
              ActiveSheet.Paste
              ActiveSheet.Name = TextFile.Name
              Application.CutCopyMode = False
  
              TextFile.Close
  
          Next i
  
      End If
  
      Application.ScreenUpdating = False
  End Sub
  </code></pre>
  
  <p><strong><em>SOLUTIONS</em></strong></p>
  
  <pre><code>Public OpenFiles As Variant
  Public FileNumber As Integer
  Public TextFile As Workbook
  Public FileNumberString As String
  
  Public Function IsArrayAllocated(Arr As Variant) As Boolean
  On Error Resume Next
  IsArrayAllocated = IsArray(Arr) And _
                      Not IsError(LBound(Arr, 1)) And _
                      LBound(Arr, 1) &lt;= UBound(Arr, 1)
  End Function
  Public Sub OpenFile()
      OpenFiles = Application.GetOpenFilename(FileFilter:="Text Files, *.txt", Title:="Select File(s) to Import", MultiSelect:=True)
      FileNumber = Application.CountA(OpenFiles)
      FileNumberString = CStr(FileNumber)
      MsgBox (IsArrayAllocated(OpenFiles))
      If IsArrayAllocated(OpenFiles) = False Then
          MsgBox ("You have selected 0 Files")
      Else:
          MsgBox ("You have selected " + FileNumberString + " Files")
      End If
  End Sub
  </code></pre>