1

我使用 GetFolder 函数来组合几个 excel 文件。

Folder = GetFolder()
Folder = Folder & "\"
FName = Dir(Folder & "*.xl*")
...

Function GetFolder()
     'Declare a variable as a FileDialog object.
  Dim fd As FileDialog

  'Create a FileDialog object as a Folder Picker dialog box.
  Set fd = Application.FileDialog(msoFileDialogFolderPicker)
  fd.Title = "Select Excel Workbook(s) Folder"
  'Declare a variable to contain the path
  'of each selected item. Even though the path is a String,
  'the variable must be a Variant because For Each...Next
  'routines only work with Variants and Objects.
  Dim vrtSelectedItem As Variant

  'Use a With...End With block to reference the FolderDialog object.
  With fd

    'Use the Show method to display the File Picker dialog box and return the user's action.
    'The user pressed the action button.
    If .Show = -1 Then

      'Step through each string in the FileDialogSelectedItems collection.
      For Each vrtSelectedItem In .SelectedItems

        'vrtSelectedItem is a String that contains the path of each selected item.
        'You can use any file I/O functions that you want to work with this path.
        'This example simply displays the path in a message box.
        GetFolder = vrtSelectedItem

      Next vrtSelectedItem
    'The user pressed Cancel.
    Else
    End If
  End With

  'Set the object variable to Nothing.
  Set fd = Nothing


End Function

当我激活该功能时,会打开一个 Windows 浏览器窗口,使我能够在我的计算机上选择所需的文件夹,然后继续执行其余代码。问题是当我想在不选择文件夹的情况下关闭浏览器时,我单击右上角的关闭 (X) 按钮,窗口确实关闭了,但代码继续运行,就好像我选择了一个文件夹一样。我怎样才能让它关闭并“结束子”?

4

1 回答 1

1

尝试If Statement在调用后立即添加GetFolder function,如下所示:

If IsEmpty(Folder) Then Exit Sub
于 2013-08-05T07:44:10.680 回答