首页 文章

添加'Excel sheet updated'消息框

提问于
浏览
2

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

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

用于更新电子表格并保存到另一个工作表的命令按钮 .

如果命令按钮由于某种原因无法工作,是否仍会显示此消息?如果是这样,我怎样才能更改到消息框,这样只有在电子表格明确更新时才能使用?

2 回答

  • 2

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

    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
    
  • 1

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

    这是一个资源,您可以在其中找到有关如何设置的信息: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
    

相关问题