Excel UDF 检测分页符?
我正在尝试编写一个返回单元格是否处于分页符的 UDF。
到目前为止,我有这样的情况:
Function pbreak() As Boolean
' Application.Volatile
pbreak = False
Dim ra As Range
Set ra = Application.Caller
With ra
For i = 1 To .Worksheet.HPageBreaks.Count
If .Worksheet.HPageBreaks(i).Location.Row = .Row Then
pbreak = True
End If
Next
End With
End Function
这会返回 #VALUE
错误。我尝试对其进行调试,HPageBreaks.Count
返回 3(并且有 3 个分页符),但是 HPageBreaks(i)
产生“索引超出范围”错误对于当前单元格下方的所有分页符。
这是一个错误(即 .Count 错误),还是我缺少一些分页符的特殊行为?
有没有办法解决这个问题(最好不要诉诸on error resume next
)?
谢谢 马丁
I'm trying to write a UDF that returns whether the cell is at a page break.
So far I have this:
Function pbreak() As Boolean
' Application.Volatile
pbreak = False
Dim ra As Range
Set ra = Application.Caller
With ra
For i = 1 To .Worksheet.HPageBreaks.Count
If .Worksheet.HPageBreaks(i).Location.Row = .Row Then
pbreak = True
End If
Next
End With
End Function
This returns a #VALUE
error. I've tried debugging it, HPageBreaks.Count
returns 3 (and there are 3 page breaks), but HPageBreaks(i)
yields an "index out of range"-error for all pagebreaks that are below the current cell .
Is this a bug (ie .Count is wrong), or is there some special behavior with page breaks that I am missing?
Is there a way to fix this (preferably without resorting to on error resume next
)?
Thanks
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:始终使用
Option Explicit
&使用前先编译代码。在循环内使用
退出函数
是为了防止代码在知道结果后进一步运行它。EDIT: Always use
Option Explicit
& compile the code before using it.Use of
Exit Function
inside the loop is to prevent the code from running it further, once the result is known.