1004:excel VBA中的应用程序定义或对象定义的错误 [英] 1004 : application-defined or object-defined error in excel VBA
问题描述
我有非常大的宏代码,它将执行以下功能之一。
我们有模板,它将描述如何生成报告以及下面的代码片段,它会收到错误。它正在执行的功能是基于模板定义,它将获取数据并将其粘贴到新工作表中。
I have very big Macro code which will do one of the below functionalities.
We have templates which will describe how the report to be generated and the below code snippet it is getting the error. The functionality which it is doing is based on the template definition it will fetch the data and paste it into new sheet.
With objCurrentWorksheet
If intTall > 1 Then
ActiveWindow.View = xlPageBreakPreview
i = 1
.PageSetup.FitToPagesTall = False
.ResetAllPageBreaks
Do While i <= .HPageBreaks.Count
Set objCell = .Range("B" & .HPageBreaks(i).Location.Row - 1)
Do Until objCell.Height < 15
Set objCell = .Range("B" & objCell.Row - 1)
Loop
Set objCell = .Range("B" & objCell.Row + 1)
>>> It is throwing the error here
.HPageBreaks.Add Before:=objCell
i = i + 1
Loop
ActiveWindow.View = xlNormalView
End If
' End If
End With
finishTemplate = True
Exit Function
< b>我尝试了什么:
我试图修改模板定义但是还没有解决
What I have tried:
I have tried to modify the template definitions but it hasn't worked out
推荐答案
使用
Do While i <= .HPageBreaks.Count
您应该从索引0开始遍历集合而不是1并在i等于之前停止计数。
或者,对每个构造使用a例如
You should start traversing the collection from index 0 not 1 and stop before i equals the count.
Alternatively, use a for each construct e.g.
Dim pb As HPageBreak
For Each pb in objCurrentWorksheet.HPageBreaks
...
对不起,错误正在消失
Sorry the error is throwing on
Set objCell = .Range("B" & .HPageBreaks(i).Location.Row - 1)
首先,请阅读以下内容:
在Excel中使用HPageBreaks或VPageBreaks.Location时收到下标超出范围错误消息 [ ^ ]
下标超出范围(错误9) [ ^ ]
好吧,你应该避免使用PageBreaks设置对单元格的引用。如果您尝试设置分页符,请使用自定义方法(自定义逻辑),但您应首先清除PageBreaks设置。
First of all, please, read these:
You receive a "Subscript out of range" error message when you use HPageBreaks or VPageBreaks.Location in Excel[^]
Subscript out of range (Error 9)[^]
Well, you should avoid setting reference to the cell by using PageBreaks. If you trying to set page breaks, use custom method (custom logic), BUT you should clear PageBreaks settings first.
这篇关于1004:excel VBA中的应用程序定义或对象定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!