2015-05-29 149 views
2

我想提示用戶在默認文件夾中打開Excel文件。我不知道如何打開默認文件夾。VBA Excel提示用戶選擇默認文件夾中的文件

Sub Program1() 
    DefaultFolder = "C:\user\dump" 
    FName = Application.GetOpenFilename 
    If FName <> False Then 
     Set WorkB2 = Workbooks.Open(FName) 
     ' some codes here 
    End If 
End Sub       

回答

5

使用CHDIR:

Sub Program1() 
    Chdir "C:\user\dump" 
    FName = Application.GetOpenFilename 
    If FName <> False Then 
    Set WorkB2 = Workbooks.Open(FName) 
    ' some codes here 
    End If 
End Sub 
1

這裏是例如打開Excel文件

Option Explicit 
Sub OpenFile() 
    Dim xlFile  As Variant 

    ChDir "C:\temp" 

    '// Showing Excel Dialog 
    xlFile = Application.GetOpenFilename("All Excel Files (*.xls*)," & _ 
    "*.xls*", 1, "Select Excel File", "Open", False) 

    '// If Cancel then exit 
    If TypeName(xlFile) = "Boolean" Then 
     Exit Sub 
    End If 

    '// Open selected file 
    Workbooks.Open xlFile 
End Sub 
+1

我認爲這是沒有問題的。 「我想提示用戶在默認文件夾**中打開Excel文件**。」如果我理解正確,他正在尋找一種方法來設置默認文件夾。看看他的企圖代碼:'DefaultFolder =「C:\用戶\突降」' – vacip

+0

哎呀我想我需要讓我的咖啡錯誤@vacip – 0m3r

+0

我歡樂的U提到的這種方法,我從來沒有使用'應用。 FileDialog(msoFileDialogOpen)',所以下次我會再試一試。 :) – vacip

相關問題