我在vba中打开工作簿时如何停止循环

时间:2015-03-19 13:00:05

标签: 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 个答案:

答案 0 :(得分: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

答案 1 :(得分:-2)

我想通了....这是我的代码供将来参考

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

将执行ext ..