Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 创建一个函数以获取工作表的页码_Excel_Vba - Fatal编程技术网

Excel 创建一个函数以获取工作表的页码

Excel 创建一个函数以获取工作表的页码,excel,vba,Excel,Vba,我正在尝试创建一个工作表的页面索引,它有大约1500行来追溯信息。我的想法是建立一个函数或代码块来实现这个函数。不幸的是,两者都不起作用。我添加到程序中的代码是由Allen Wyatt()编写的。如果使用MsgBox显示页码,则该选项有效。我想要,它作为一个函数工作,通过它我可以得到一个随机单元格的页码(只是输入这个工作表的单元格地址),或者将它集成到我的循环程序中,用页码填充索引 我不明白为什么这两种方法都不起作用。作为功能,它只显示无效值。作为循环chaptern编号的值,我只得到页码为1

我正在尝试创建一个工作表的页面索引,它有大约1500行来追溯信息。我的想法是建立一个函数或代码块来实现这个函数。不幸的是,两者都不起作用。我添加到程序中的代码是由Allen Wyatt()编写的。如果使用MsgBox显示页码,则该选项有效。我想要,它作为一个函数工作,通过它我可以得到一个随机单元格的页码(只是输入这个工作表的单元格地址),或者将它集成到我的循环程序中,用页码填充索引

我不明白为什么这两种方法都不起作用。作为功能,它只显示无效值。作为循环chaptern编号的值,我只得到页码为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
公共函数showpagenumber()为整数
将iPages设置为整数
作为整数的Dim-iCol
作为整数的Dim-icol
暗淡的光线和长的一样
暗淡的光线和长的一样
暗x等长
长得一样暗
将iPage设置为整数
iPages=ExecuteExcel4Macro(“Get.Document(50)”)
使用ActiveSheet
y=ActiveCell.Column
iCols=.VPageBreaks.Count
x=0
做
x=x+1
循环直到x=iCols_
或y<.VPageBreaks(x).Location.Column
iCol=x
如果y>=.VPageBreaks(x).Location.Column,则
iCol=iCol+1
如果结束
y=ActiveCell.Row
lRows=.HPageBreaks.Count
x=0
做
x=x+1
循环直到x=lRows_
或y<.HPageBreaks(x).Location.Row
lRow=x
如果y>=.HPageBreaks(x).Location.Row,则
lRow=lRow+1
如果结束
如果.PageSetup.Order=xlDownThenOver,则
iPage=(iCol-1)*(lRows+1)+lRow
其他的
iPage=(lRow-1)*(iCols+1)+iCol
如果结束
以
showpagenumber=iPage
端函数
  • 程序中的代码,这样我只得到第1页

    ...
    For i=章开始到章结束
    emptyrow=WorksheetFunction.CountA(ws2.Range(“D:D”))+1
    如果不是IsEmpty(ws1.Cells(i,“A”))和IsNumeric(ws1.Cells(i,“A”)),那么
    iPages=ExecuteExcel4Macro(“Get.Document(50)”)
    使用ws1
    y=ActiveCell.Column
    iCols=.VPageBreaks.Count
    x=0
    做
    x=x+1
    循环直到x=iCols_
    或y<.VPageBreaks(x).Location.Column
    iCol=x
    如果y>=.VPageBreaks(x).Location.Column,则
    iCol=iCol+1
    如果结束
    y=ActiveCell.Row
    lRows=.HPageBreaks.Count
    x=0
    做
    x=x+1
    循环直到x=lRows_
    或y<.HPageBreaks(x).Location.Row
    lRow=x
    如果y>=.HPageBreaks(x).Location.Row,则
    lRow=lRow+1
    如果结束
    如果.PageSetup.Order=xlDownThenOver,则
    iPage=(iCol-1)*(lRows+1)+lRow
    其他的
    iPage=(lRow-1)*(iCols+1)+iCol
    如果结束
    以
    ws2.单元格(空“D”)。值=iPage
    如果结束
    下一个
    

  • 从即时窗口调用函数时,函数
    showpagenumber
    确实有效。当代码按照Allen Wyatt的建议实现时,消息框也在工作。但是这里有严重的错误……注释行iPages=ExecuteExcel4Macro(“Get.Document(50)”),您的函数就可以工作了!别问我为什么!谢谢你的解释。至少我现在知道,什么是行不通的,也不会在进一步的解决方案上浪费时间。
        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