2017-03-07 220 views
0

我有以下代码和YesNoCancel选项不会执行任何操作。请问我做错了什么?VBA YesNoCancel不按预期方式工作

Option Explicit 

Sub wwb() 

    'lists each book that's OPEN 
    Dim wb As Workbook, ws As Workbook, wd As Workbook 
    Set wd = ThisWorkbook 
     MsgBox wd.Name 
    Dim output As Integer 
    Dim msgValue 
    For Each wb In Application.Workbooks 
     If wb.Name = wd.Name Then 
      MsgBox "The destination WorkBook is :" & wd.Name 
     Else 
      output = MsgBox("Is " & wb.Name & " your source file to import data?", vbYesNoCancel, "Please confirm source file") 
       If msgValue = vbYes Then 
        MsgBox "test yes" 
       ElseIf msgValue = vbNo Then 
        MsgBox "test No" 
       ElseIf msgValue = vbCancel Then 
        MsgBox "Test cancel" 
       End If 
     End If 
    Next wb 

End Sub 
+0

您不必为'wb'一个'set'。谁是'wb'? – BOB

+0

vb是所有打开的工作簿,并且只在for循环中存储临时名称,如下定义:For Each wb在Application.Workbooks –

回答

1

您需要检查的output代替msgValue

output = MsgBox("Is " & wb.Name & " your source file to import data?", vbYesNoCancel, "Please confirm source file") 
     If output = vbYes Then 
      MsgBox "test yes" 
     ElseIf output = vbNo Then 
      MsgBox "test No" 
     ElseIf output = vbCancel Then 
      MsgBox "Test cancel" 
     End If 
+0

新增功能非常明显,非常感谢Paul,它像一个魅力 –