Excel MsgBox:使用“If Not then Else End If”与 vbOkCancel

问题描述 投票:0回答:2

尝试在程序开始时检查并设置窗口缩放级别。 仅当窗口缩放级别设置为 200% 时,该过程才会启动。

试着说:

  • 检查窗口缩放级别
  • 如果当前设置为 200,则启动
  • 如果不是,请使用 MsgBox 告知用户即将发生的更改,并提供两个选项:
    • 确定 = 将缩放级别更改为 200 并开始
    • 取消 = 不更改并退出

如果当前级别为“正确”,则到目前为止的代码才会开始。

如何构建它,以便它在更改缩放级别后也启动?

Sub AddChart()
  '... 
  If Not ActiveWindow.Zoom = 200 Then
    MsgBox "Set Window Zoom to: 200%", vbOKCancel
  Else
  ActiveWindow.Zoom = 200
  Set ws = ActiveSheet
  With ws
  '...
  End With
  End If
End Sub
excel vba if-statement msgbox
2个回答
1
投票

您的代码测试缩放是否设置为 200。如果不是,它会警告您更改它。如果是的话,无论如何都会设置它..?

您应该使用:

If Not ActiveWindow.Zoom = 200 Then
    MsgBox "Setting Window Zoom to: 200%", vbOKOnly
    ActiveWindow.Zoom = 200
End If

0
投票
  • 按照@CLR建议使用
    vbOkOnly
Sub AddChart()
  '... 
    If Not ActiveWindow.Zoom = 200 Then
        MsgBox "Setting Window Zoom to: 200%", vbOKOnly, "Info"
        ActiveWindow.Zoom = 200
    End If
  Set ws = ActiveSheet
  With ws
  '...
  End With
End Sub
  • 使用
    vbOkCancel
Sub AddChart()
  '... 
  Dim Result As Integer
  If Not ActiveWindow.Zoom = 200 Then
  Result = MsgBox("Set Window Zoom to: 200%", vbOKCancel, "Info")
    If Result = vbOK Then
       ActiveWindow.Zoom = 200
    Else: Exit Sub
    End If
  End If
  Set ws = ActiveSheet
  With ws
  '...
  End With
End Sub 
© www.soinside.com 2019 - 2024. All rights reserved.