简单的Excel VBA – 缩放恢复到页面默认

当前macros

Private Sub Worksheet_SelectionChange If Target.Address = "$C$11" Then ActiveWindow.Zoom = 120 Else ActiveWindow.Zoom = 55 End If End Sub 

所需的macros

点击单元格C11,放大到120,否则,缩放应该是无论表单设置为。 我使用55,但其他人可能会使用75,我不希望我的macros停止。 想法?

你可以试试这个代码:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Static zoomLevel As Integer 'Ensure zoomLevel is set If zoomLevel = 0 Then zoomLevel = ActiveWindow.Zoom End If 'If cell C11 is selected, zoom on If Target.Address = "$C$11" Then zoomLevel = ActiveWindow.Zoom ActiveWindow.Zoom = 120 Else 'If any other cell is selected zoom to the user's zoomLevel ActiveWindow.Zoom = zoomLevel End If End Sub 

使用Static允许variables在子程序执行结束后保持其值。