在Excel

时间:2017-05-05 14:01:25

标签: excel vba excel-vba shell

我一直在研究使用 WScript.CreateObject(" WScript.Shell")来充当创建多行,悬停,5秒钟流行音乐的方法用户表单上的命令按钮的特写。从我能想到的一切来看,这应该可以正常工作,但它会给出"变量未定义" 错误。在调试时,它会在"WScript"行中突出显示Set作为问题。

Private Sub CB1604A_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
                              ByVal X As Single, ByVal Y As Single)

Dim WshShell
Dim BTN
Set WshShell = WScript.CreateObject("WScript.Shell")

BTN = WshShell.PopUp("What do you want to do?", 5)

Select Case BTN
    Case 6
        WScript.Echo "Do it now."
    Case 7
        WScript.Echo "Do it later."
    Case -1
        WScript.Echo "Cancel all actions?"
End Select

End Sub

1 个答案:

答案 0 :(得分:1)

您需要添加对WScript的引用才能使用其CreateObject - 但您并不需要;而是使用VBA CreateObject来创建.Shell的实例:

Set WshShell = CreateObject("WScript.Shell")
BTN = WshShell.PopUp("What do you want to do?", 5)

然后使用MsgBox代替WScript.Echo

...
MsgBox "Do it now."