添加“Excel工作表更新”消息框

我已经使用代码添加了一个消息框

If MsgBox("File has been updated", vbinformatoin) = ok Then Exit Sub 

直到用于更新电子表格并保存到另一张表格的命令button。

如果命令button由于某种原因而不工作,这个消息还会出现吗? 如果是这样,我怎么可以改变到消息框,所以它只能在电子表格肯定更新时才起作用?

你需要的是一个error handling程序。 你需要在你的子开头设置一个“On Error”语句,然后写一个error handling程序。

这里有一个资源,你可以find有关如何设置它的信息: https : //support.microsoft.com/en-us/kb/141571#/en-us/kb/141571

你的代码会是这样的

 Sub mySub() On Error GoTo CatchError 'I called it CatchError but you can call it whatever '--All of your code here including "If MsgBox("File has been updated", vbinformatoin) = ok Then Exit Sub" CatchError: 'Here put what you want to do in case of an error, for example 'MsgBox "The code stopped because of an error" End Sub 

我不确定这是否完全回答你的问题,但你可以试试这个:
(注意:代替消息框替代相关的代码)

 Private change_ind As Integer Private Sub Worksheet_Change(ByVal Target As Excel.Range) change_ind = 1 End sub Private Sub CommandButton1_Click() If change_ind = 1 Then MsgBox "Update has occurred" Else   MsgBox "No updates" End If End Sub