excel - 将缩放视图自动调整到表格中的活动/可见单元格?
问题描述
除了根据分辨率自动更改之外,我找不到任何 vba 缩放,但是是否可以根据最远的包含文本的列来自动调整自定义缩放级别?
Sub Workbook_Open()
ActiveWindow.Zoom = 100 'also you can change to other size
End Sub
奖金代码:
要将滚动条重置到最左侧,因此它正在查看 A/Row1 列,此代码有效:) 我将它放在“重置”用户按钮上。
'Scroll to a specific row and column
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
先感谢您。
解决方案
试试这个代码:
Function FindFurthestColumn(S As Worksheet) As Integer
Dim CellsWithContent As Long
CellsWithContent = WorksheetFunction.CountA(S.Cells)
If CellsWithContent = 0 Then
FindFurthestColumn = 1
Exit Function
End If
Dim CellsCount As Long
Dim j As Integer
Do
j = j + 1
CellsCount = CellsCount + WorksheetFunction.CountA(S.Columns(j))
Loop Until CellsCount = CellsWithContent
FindFurthestColumn = j
End Function
Function CellIsVisible(cell As Range) As Boolean
CellIsVisible = Not Intersect(ActiveWindow.VisibleRange, cell) Is Nothing
End Function
Sub ZoomVisibleCells()
Application.ScreenUpdating = False
Dim LastColumn As Integer
LastColumn = FindFurthestColumn(ActiveSheet)
Dim SplitCell As Range
If ActiveWindow.Split = True Then
Set SplitCell = Cells(ActiveWindow.SplitRow + 1, ActiveWindow.SplitColumn + 1)
ActiveWindow.FreezePanes = False
End If
Dim Zoom As Integer
For Zoom = 400 To 10 Step -1
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
ActiveWindow.Zoom = Zoom
If CellIsVisible(ActiveSheet.Cells(1, LastColumn + 1)) Then
Exit For
End If
Next Zoom
If Not SplitCell Is Nothing Then
SplitCell.Activate
ActiveWindow.FreezePanes = True
End If
Application.ScreenUpdating = True
End Sub
CellIsVisible 函数的功劳: https ://stackoverflow.com/a/11943260/14370454
推荐阅读
- java - 运行 Java 应用程序时 GPU 使用率高
- javascript - 如何在此设置中使用 array.map()?
- zsh - 编辑多行输入时zsh中一致的PS2提示?
- javascript - 如何定位不同窗口、相同域中的元素?
- c++ - Visual Studio 的默认工作 .clang 格式文件
- android - 如何从自定义属性中读取维度值?
- python - Python:如何从子目录导入包
- c# - 如何使用协商身份验证方法从 ASP.NET Core Web 应用程序连接到 SQL Server?
- google-apps-script - 在脚本创建的电子邮件中嵌入一个按钮
- html - 如何将导航栏品牌和导航栏与 bootstrap 5 对齐?