EXCEL如何在单元格中获取打印分页的当前页数和总页数

2024-12-16 20:09:27
推荐回答(1个)
回答1:

只能用VBA解决,下面的代码由 cbtaja 录制,将下面代码粘贴进 工具--宏--VB编辑器后,用公式 =ThisPageNo 显示当前页数,=PagesCount显示总页数;=TEXT(ThisPageNo,"第0页 ")&TEXT(PagesCount,"共0页") 在同一单元格显示当前页数和总页数

Sub 定义页码及总页数名称()
'
' 定义页码及总页数名称 Macro

'

'
ActiveWorkbook.Names.Add Name:="ColFirst", RefersToR1C1:= _
"=GET.DOCUMENT(61)" '判断打印顺序的设置类型
ActiveWorkbook.Names.Add Name:="lstRow", RefersToR1C1:= _
"=GET.DOCUMENT(10)" '本工作表已用到的最大行数
ActiveWorkbook.Names.Add Name:="lstColumn", RefersToR1C1:= _
"=GET.DOCUMENT(12)" '本工作表已用到的最大列数
ActiveWorkbook.Names.Add Name:="hNum", RefersToR1C1:= _
"=IF(ISERROR(FREQUENCY(GET.DOCUMENT(64),Row())),0,FREQUENCY(GET.DOCUMENT(64),Row()))" 'hNum为本单元格上方的水平分页符个数
ActiveWorkbook.Names.Add Name:="vNum", RefersToR1C1:= _
"=IF(ISERROR(FREQUENCY(GET.DOCUMENT(65),Column())),0,FREQUENCY(GET.DOCUMENT(65),Column()))" ''本单元格左边的垂直分页个数
ActiveWorkbook.Names.Add Name:="hSum", RefersToR1C1:= _
"=IF(ISERROR(FREQUENCY(GET.DOCUMENT(64),lstRow)),0,FREQUENCY(GET.DOCUMENT(64),lstRow))" ''本工作表最后一个单元格上方的水平分页符个数
ActiveWorkbook.Names.Add Name:="vSum", RefersToR1C1:= _
"=IF(ISERROR(FREQUENCY(GET.DOCUMENT(65),lstColumn)),0,FREQUENCY(GET.DOCUMENT(65),lstColumn))" ''本工作表最后一个单元格左边的垂直分页个数
ActiveWorkbook.Names.Add Name:="ThisPageNo", RefersToR1C1:= _
"=IF(ColFirst,(hSum+1)*vNum+hNum+1,(vSum+1)*hNum+vNum+1)*ISNUMBER(NOW())" '单元格所在页码
ActiveWorkbook.Names.Add Name:="PagesCount", RefersToR1C1:= _
"=GET.DOCUMENT(50)*ISNUMBER(NOW())" '本工作表的总页数
End Sub