生成函数以获取工作表的页码

时间:2019-02-25 14:23:22

标签: excel vba

我正在尝试创建一个工作表的页面索引,该工作表具有大约1500行以追溯信息。我的想法是建立一个功能或一个代码块来实现此功能。不幸的是,两者都不起作用。我添加到程序中的代码是由Allen Wyatt(https://excelribbon.tips.net/T011581_Page_Numbers_in_VBA.html)编写的。如果页面编号与MsgBox一起显示,则可以使用。我想要它可以作为一个函数使用,它可以获取随机单元的页码(仅输入此工作表的单元地址),或者将其集成到我的循环程序中以用页码填充索引。

我不明白为什么两种方法都不起作用。作为功​​能,它仅显示无效值。作为循环章节编号的值,我只能将页面编号设为1。

任何一位上师可以向我解释原因吗?

非常感谢!

1。功能:

Public Function showpagenumber() As Integer

    Dim iPages As Integer
    Dim iCol As Integer
    Dim iCols As Integer
    Dim lRows As Long
    Dim lRow As Long
    Dim x As Long
    Dim y As Long
    Dim iPage As Integer


    iPages = ExecuteExcel4Macro("Get.Document(50)")

    With ActiveSheet
        y = ActiveCell.Column
        iCols = .VPageBreaks.Count
        x = 0
        Do
            x = x + 1
        Loop Until x = iCols _
          Or y < .VPageBreaks(x).Location.Column
        iCol = x
        If y >= .VPageBreaks(x).Location.Column Then
            iCol = iCol + 1
        End If

        y = ActiveCell.Row
        lRows = .HPageBreaks.Count
        x = 0
        Do
            x = x + 1
        Loop Until x = lRows _
          Or y < .HPageBreaks(x).Location.Row
        lRow = x
        If y >= .HPageBreaks(x).Location.Row Then
            lRow = lRow + 1
        End If

        If .PageSetup.Order = xlDownThenOver Then
            iPage = (iCol - 1) * (lRows + 1) + lRow
        Else
            iPage = (lRow - 1) * (iCols + 1) + iCol
        End If
    End With

   showpagenumber = iPage

End Function
  1. 程序中的代码,我只得到页码1。

    ...

        For i = chapterstart To chapterend
        emptyrow = WorksheetFunction.CountA(ws2.Range("D:D")) + 1
            If Not IsEmpty(ws1.Cells(i, "A")) And IsNumeric(ws1.Cells(i, "A")) Then
                    iPages = ExecuteExcel4Macro("Get.Document(50)")
                    With ws1
                        y = ActiveCell.Column
                        iCols = .VPageBreaks.Count
                        x = 0
                        Do
                            x = x + 1
                        Loop Until x = iCols _
                          Or y < .VPageBreaks(x).Location.Column
                        iCol = x
                        If y >= .VPageBreaks(x).Location.Column Then
                            iCol = iCol + 1
                        End If
    
                        y = ActiveCell.Row
                        lRows = .HPageBreaks.Count
                        x = 0
                        Do
                            x = x + 1
                        Loop Until x = lRows _
                          Or y < .HPageBreaks(x).Location.Row
                        lRow = x
                        If y >= .HPageBreaks(x).Location.Row Then
                            lRow = lRow + 1
                        End If
    
                        If .PageSetup.Order = xlDownThenOver Then
                            iPage = (iCol - 1) * (lRows + 1) + lRow
                        Else
                            iPage = (lRow - 1) * (iCols + 1) + iCol
                        End If
                    End With
    
                    ws2.Cells(emptyrow, "D").Value = iPage
    
            End If
        Next
    

0 个答案:

没有答案