是否可以根据屏幕分辨率设置缩放级别,而无需 Select
?
我已经遵循了代码:
Sheets(1).Range("A1:AC1").Select
ActiveWindow.Zoom = True
取自https://stackoverflow.com/a/19439177/1903793
所需的代码类似于:
Range("A1:AC1").Width.Zoom=True
更新。为什么我要避免选择?
- 我的工作表包含基于用户设置的隐藏列。因此 A1:AC1 范围内的某些列被隐藏。我无法选择单个列,因为该特定列可能被隐藏。
- 选择会触发事件。当然,我可以禁用事件,但禁用事件会产生一些我想避免的副作用。
最佳答案
只需测量当前窗口宽度和范围宽度。然后您可以使用这些值来设置缩放比例。注意 - 这需要一些额外的验证和错误处理,但它应该给出基本的想法。
Private Sub ZoomToRange(target As Range)
'Get the window from the target range.
Dim wnd As Window
Set wnd = ActiveWindow
'Find out what you need to scale to.
Dim scaling As Long
scaling = 100 * wnd.Width / target.Width
'Limit to max and min zoom level.
If scaling > 400 Then
wnd.Zoom = 400
ElseIf scaling < 10 Then
wnd.Zoom = 10
Else
wnd.Zoom = scaling
End If
'Scroll to the upper left cell
target.Cells(1, 1).Activate
End Sub
关于excel - 在Excel VBA中设置缩放而不选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39331980/