Man*_*anu 5 excel vba excel-vba
我有一个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
Run Code Online (Sandbox Code Playgroud)
我将在工作簿上将此模块称为
Private Sub Workbook_Open()
ScreenRes
End Sub
Run Code Online (Sandbox Code Playgroud)
小智 5
使用Worksheets集合选择所有工作表对象,Application.ActiveWindow属性将指向它们全部。
With Worksheets
.Select
ActiveWindow.Zoom = 75
End With
Run Code Online (Sandbox Code Playgroud)
基于 @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
Run Code Online (Sandbox Code Playgroud)