2013-08-05 26 views
1

林。关闭浏览器窗口,为了把几个Excel文件使用的GetFolder功能出类拔萃

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)按钮,窗口关闭,但代码继续,就像我选择了一个文件夹一样。 我怎样才能使它关闭和“结束小组”?

回答

1

你叫GetFolder function之后,这样的尝试简单地增加If Statement

If IsEmpty(Folder) Then Exit Sub 
+0

我希望它关闭,当我点击X(关闭)按钮,而不是当文件夹为空 – user1040563

+0

@ user1040563,如果按X然后你的'文件夹变量'是空的 - 这就是我检查的内容。我不检查文件夹中是否有任何内容。添加这一行,并检查它是不是你正在寻找。 –

+0

对不起,你是对的。非常感谢你 – user1040563