2015-03-19 61 views
0

我的代碼打開工作簿在vba中,但是當我取消或不想打開工作簿時,它不取消或關閉。這裏是我的代碼可以你們,請給我任何建議...如何停止循環當我打開工作簿在vba

Dim shname As String 
    Dim wb As String 
    wb = Application.GetOpenFilename 
     If wb <> "False" Then Workbooks.Open wb (this part is giving me prompt to open workbook) 
     If wb = "False" Then workbooks.Cancel wb (this part i want it to close the prompt when i click cancel instead of open....) 

回答

-2

我想通了.. ..這是我以供將來參考

  If workbookPath = "False" Then 
     MsgBox "You chose ...." 
      Exit sub 

這將分機執行..

+0

讓 - 弗朗索瓦有答案,你應該已經標記爲答案... – vacip 2015-07-05 10:03:28

1

你的代碼工作正常,但你似乎並不理解它。

Dim wb As String 
wb = Application.GetOpenFilename 
If wb <> "False" Then Workbooks.Open wb '(this part is giving me prompt to open workbook) 

(這部分是給我的提示,打開工作簿)

沒有! Application.GetOpenFilename給你提示。

然而,這條線是不必要的(並且將無法正常工作),所以只是將其刪除:

If wb = "False" Then workbooks.Cancel wb '(this part i want it to close the prompt when i click cancel instead of open....) 

我會重新組織你的代碼是這樣的:

Dim workbookPath As String 
workbookPath = Application.GetOpenFilename 
If workbookPath = "False" Then 
    'User clicked cancel. Do nothing. 
    MsgBox "You chose not to open a workbook." 
Else 
    'User chose a workbook to open. Open it. 
    Workbooks.Open workbookPath 
End If