gpt4 book ai didi

excel - 打开 Excel 文件时未设置对象变量或 With block 变量

转载 作者:行者123 更新时间:2023-12-03 03:16:42 27 4
gpt4 key购买 nike

因为我找不到增加下拉列表字体大小的方法,所以我必须找到解决方法。一种选择是放大。因此我有这段代码 - 效果很好。但是,一旦我关闭文件并尝试重新打开它,就会出现一条错误消息:“未设置对象变量或 block 变量。”

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo errorHandler

Dim xZoom As Long
xZoom = 60

If Target.Validation.Type = xlValidateList Then xZoom = 125

errorHandler:
ActiveWindow.Zoom = xZoom 'Debug highlights this row

End Sub

我尝试过事件:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo errorHandler

Dim wb as Workbook

Dim xZoom As Long
xZoom = 60

Set wb = ThisWorkbook

wb.Activate

If Target.Validation.Type = xlValidateList Then xZoom = 125

errorHandler:
ActiveWindow.Zoom = xZoom 'Debug highlights this row

End Sub

但似乎没有任何作用...我根本不知道原因可能是什么...有人可以帮助我吗?

最佳答案

在进行缩放之前检查 windows.count > 0。

If Application.Windows.Count > 0 Then
ActiveWindow.Zoom = xZoom
End If

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo errorHandler

Dim xZoom As Long
xZoom = 60

If Target.Validation.Type = xlValidateList Then
xZoom = 125
If Application.Windows.Count > 0 Then
ActiveWindow.Zoom = xZoom 'Debug highlights this row
End If
End If
Exit Sub 'must exit before the error handler
errorHandler:
Msgbox(Err.Description)
End Sub

关于excel - 打开 Excel 文件时未设置对象变量或 With block 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com