VBA在Sheets上设置缩放级别

时间:2016-09-27 03:58:50

标签: vba excel-vba excel

我有一个VBA,它将根据屏幕分辨率设置缩放级别。 但是当你打开工作簿时它只适用于ActiveWindow。 如何在Excel中的所有工作表中添加此内容?

Declare Function GetSystemMetrics32 Lib "user32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long

Public Sub ScreenRes()
    Dim lResWidth As Long
    Dim lResHeight As Long
    Dim sRes As String

    lResWidth = GetSystemMetrics32(0)
    lResHeight = GetSystemMetrics32(1)
    sRes = lResWidth & "x" & lResHeight
    Select Case sRes
        Case Is = "800x600"
            ActiveWindow.Zoom = 75
        Case Is = "1024x768"
            ActiveWindow.Zoom = 125
        Case Else
            ActiveWindow.Zoom = 100
    End Select
End Sub

我将在工作簿上调用此模块

Private Sub Workbook_Open()
ScreenRes
End Sub

2 个答案:

答案 0 :(得分:4)

使用Worksheet Objects选择所有Worksheets collectionApplication.ActiveWindow property将指向所有人。

With Worksheets
    .Select
    ActiveWindow.Zoom = 75
End With

答案 1 :(得分:1)

在@Jeeped回答的基础上,您可以在ThisWorkbook代码窗格中输入以下代码:

Declare Function GetSystemMetrics32 Lib "user32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long

Option Explicit

Private Sub Workbook_Open()
    With Worksheets
        .Select
        ActiveWindow.zoom = ScreenResToZoom
    End With
End Sub

Public Function ScreenResToZoom() As Long
    Select Case GetSystemMetrics32(0) & "x" & GetSystemMetrics32(1)
        Case Is = "800x600"
            ScreenResToZoom = 75
        Case Is = "1024x768"
            ScreenResToZoom = 125
        Case Else
            ScreenResToZoom = 100
    End Select
End Function