excel - 在Excel VBA中设置缩放而不选择

标签 excel vba zooming active-window

是否可以根据屏幕分辨率设置缩放级别,而无需 Select

我已经遵循了代码:

Sheets(1).Range("A1:AC1").Select
ActiveWindow.Zoom = True

取自https://stackoverflow.com/a/19439177/1903793

所需的代码类似于:

Range("A1:AC1").Width.Zoom=True

更新。为什么我要避免选择?

  1. 我的工作表包含基于用户设置的隐藏列。因此 A1:AC1 范围内的某些列被隐藏。我无法选择单个列,因为该特定列可能被隐藏。
  2. 选择会触发事件。当然,我可以禁用事件,但禁用事件会产生一些我想避免的副作用。

最佳答案

只需测量当前窗口宽度和范围宽度。然后您可以使用这些值来设置缩放比例。注意 - 这需要一些额外的验证和错误处理,但它应该给出基本的想法。

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/

相关文章:

swift - 如何在代码创建的 ScrollView 上捏放大

regex - 在 VBA 中返回正则表达式匹配(excel)

vba - VISIO VBA 获取组中所有形状的集合

vba - 具有特殊字符 "/"的对象在 GetObject 中产生自动化错误

excel - 传递函数变量

html - CSS 调整图像大小

android - 如何将缩放控件设置为自定义按钮?

excel - 从 Outlook 引用和访问 Excel

excel - Excel 中以日期为条件的条件查找

Java - 从剪贴板检索 XML (Excel) 电子表格