Excel UDF 检测分页符?

发布于 2024-10-11 16:37:46 字数 701 浏览 7 评论 0原文

我正在尝试编写一个返回单元格是否处于分页符的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2024-10-18 16:37:46
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

编辑:始终使用 Option Explicit &使用前先编译代码。
在循环内使用退出函数是为了防止代码在知道结果后进一步运行它。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文