Search code examples
excelvbaif-statementmsgbox

Excel MsgBox Ok/Cancel


I have a procedure that should only start if a condition is met.

In this example the condition is ActiveWindow.Zoom = 200

Trying to use a MsgBox with vbOkCancel to say:

  • check the window zoom level:
    • if it's already set to 200: start
    • if not: inform the user and give two options:
      • Ok = change zoom level to 200 and start
      • Cancel = don't change and exit

Issue: The code starts the procedure, if the active window zoom is already set to 200. Otherwise, it only sets the zoom to 200 and exits.

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

Solution

  • Your code tests if the Zoom is set to 200. If it is not, it warns you to change it. If it is, it sets it anyway..?

    You should use:

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