Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 添加';Excel表格更新';消息框_Vba_Excel - Fatal编程技术网

Vba 添加';Excel表格更新';消息框

Vba 添加';Excel表格更新';消息框,vba,excel,Vba,Excel,我已经使用代码添加了一个消息框 If MsgBox("File has been updated", vbinformatoin) = ok Then Exit Sub 打开用于更新电子表格并保存到其他工作表的命令按钮 如果命令按钮因某种原因无法工作,是否仍会显示此消息?如果是这样,我如何才能更改为消息框,使其仅在电子表格已明确更新时才起作用?您需要的是一个错误处理程序。您需要在sub的开头设置一个“On Error”语句,然后编写一个错误处理程序 以下是一个资源,您可以在其中找到有关如何设置

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

If MsgBox("File has been updated", vbinformatoin) = ok Then Exit Sub
打开用于更新电子表格并保存到其他工作表的命令按钮


如果命令按钮因某种原因无法工作,是否仍会显示此消息?如果是这样,我如何才能更改为消息框,使其仅在电子表格已明确更新时才起作用?

您需要的是一个错误处理程序。您需要在sub的开头设置一个“On Error”语句,然后编写一个错误处理程序

以下是一个资源,您可以在其中找到有关如何设置它的信息:

你的代码应该是这样的

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