gpt4 book ai didi

excel - 某些工作表上出现运行时错误 '1004' : Method 'Range' of object 'Global' failed,,但我的代码能够在大多数其他工作表上运行?

转载 作者:行者123 更新时间:2023-12-03 02:17:34 25 4
gpt4 key购买 nike

我是 VBA 新手,正在多个工作表上运行代码。我的代码在大多数工作表中运行良好,但是当它循环到包含我从数据库导出的数据的工作表时,我收到一个运行时错误,该工作表的格式不是标准的 Excel 工作表(有数字)格式为“自定义”)。如果 Excel 由于工作表的格式设置而无法将工作表中的数字识别为属于该范围的数字,这是否会导致错误?请参阅下面的代码中不起作用的部分。它适用于大多数工作表,但当它到达格式与其他工作表不同的选项卡时,我会收到错误。

Dim rng As Range

With ActiveSheet.PageSetup
Set rng = Range(.PrintArea)
End With

最佳答案

来自PageSetup.PrintArea文档:

Set this property to False or to the empty string ("") to set the print area to the entire sheet

如果未设置打印区域,则 .PrintArea 的结果(空白字符串)不是有效的 Range 引用:

尝试以下操作:

With ActiveSheet.PageSetup
If .PrintArea = vbNullString Then
Set rng = ActiveSheet.UsedRange
Else
Set rng = ActiveSheet.Range(.PrintArea)
End If
End With

关于excel - 某些工作表上出现运行时错误 '1004' : Method 'Range' of object 'Global' failed,,但我的代码能够在大多数其他工作表上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57963841/

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