Excel自定义项检测分页?

Excel自定义项检测分页?,excel,vba,excel-2003,Excel,Vba,Excel 2003,我试图编写一个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.HP

我试图编写一个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
这将返回一个
#值
错误。我试过调试它,
HPageBreaks.Count
返回3(有3个分页符),但是
HPageBreaks(I)
会对当前单元格下面的所有分页符产生“索引超出范围”错误

这是一个bug(即Count是错误的),还是有一些特殊的行为与分页符,我错过了

是否有办法解决此问题(最好不要在下一次错误恢复时诉诸

谢谢 Martin

选项显式
函数pbreak()为布尔函数
'应用程序.易失性
Dim i作为“缺失行”的整数
pbreak=False
调光范围
Set ra=Application.Caller
与ra
对于i=1到.Worksheet.HPageBreaks.Count

If.Worksheet.HPageBreaks(i).Location.Row似乎有效,谢谢!我仍然不明白为什么无法访问hpagebreaks进一步细分,特别是为什么(或如何)。计数与实际列表长度不同。您的代码适合我(Excel 2003)。您是否尝试使用更长的文档?我刚刚测试了它,它能用3页或更少的页面工作,比那多,它只能在最后一页工作。奇怪,是的,尝试了7次分页符。好的。我正在使用Excel2003SP3
Option Explicit

Function pbreak() As Boolean
   ' Application.Volatile
    Dim i As Integer   'the missing line
    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
                If .Worksheet.HPageBreaks(i).Location.Row = .Row Then
                    pbreak = True
                    'exit the function once a page break is found.
                    Exit Function
                End If
            Else
                Exit Function
            End If
        Next
    End With
End Function