[英]Is there a vba syntax to use within excel vba message boxes to force the user to click OK or have excel close?
我正在設置一個Excel電子表格,我想強制用戶或者在消息框中單擊“確定”接受服務條款,或者單擊“取消”關閉該程序。 我在Visual Basic中工作。 我還將“術語”宏放在工作簿中。
好吧,我做錯了辦法,把自己鎖在了自己的程序里。 我一直在重新編寫代碼,但無法將它們組合在一起。
Sub terms()
MsgBox Prompt:="By using this program, the user agrees to all Terms and Conditions as set forth herein. Click OK to accept and continue, or cancel to exit program.", Buttons:=vbOKCancel + vbExclamation, Title:="User Agreement:"
Dim Answer As VbMsgBoxResult
If Answer = vbCancel Then Workbooks.Close
If vbOK Then Workbooks.Open
End Sub
在工作簿中,我輸入了以下代碼:
Private Sub Workbook_Open()
terms
End Sub
我期望如果用戶單擊“取消”,程序將關閉,如果單擊“確定”,程序將打開。 我嘗試了各種不同的方法,最近的錯誤指出編譯器錯誤,指出參數不是可選的。
工作簿是所有Open工作簿的集合。 因此,聲明Workbooks.Open沒有任何意義,因為所有成員均已打開。 open方法以Workbooks(“ c:\\ Test \\ myspreadsheet.xls”)的形式向集合中添加一個特定的工作簿。Open(邏輯上應該是Add not Open,但我知道它們來自何處)。類似的Workbooks.Close將關閉所有打開的工作簿,而您可能只想關閉剛剛打開的文件。
另外,要從消息框中獲取答復,您還必須分配一個變量以捕獲該答復-因此
answer = msgbox("You sure?",vbyesno)
所以你真正想要的是
Sub terms()
Dim msg as string
Dim Answer As VbMsgBoxResult
msg = "By using this program, the user agrees to all Terms and Conditions as set forth herein. Click OK to accept and continue, or cancel to exit program."
Answer = MsgBox(Prompt:=msg, Buttons:=vbOKCancel + vbExclamation, Title:="User Agreement:")
If Answer = vbCancel Then ThisWorkbook.Close
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.