代码再简化一点 Sub hjs() '直接运行可以求到,当前单元格的位置所在的页数和总页数
Dim P As HPageBreak, Q As VPageBreak
Dim m%, n%, x&, y&, x1%, y1%
Dim arr(), rng As Range, Sht As Worksheet Application.ScreenUpdating = False
Set rng = ActiveCell '设置rng为当前单元格
Set Sht = ActiveSheet
If Application.Intersect(rng, Sht.UsedRange) Is Nothing Then MsgBox "当前单元格不在数据区域内,无页码": Exit Sub x = rng.Row: y = rng.Column 'x为行,y为列 For Each P In Me.HPageBreaks
m = m + 1
If x < P.Location.Row Then
x1 = m
Exit For
End If
Next For Each Q In Me.VPageBreaks
n = n + 1
If y < Q.Location.Column Then
y1 = n
Exit For
End If
Next
t1 = Sht.HPageBreaks.Count + 1
t2 = Sht.VPageBreaks.Count + 1
If x1 = 0 Then x1 = t1
If y1 = 0 Then y1 = t2 imax = t1 * t2
ReDim arr(1 To t1, 1 To t2)
k = 1
If Me.PageSetup.Order = 1 Then '先列后行,计算页码的顺序
For j = 1 To t2
For i = 1 To t1
arr(i, j) = k
k = k + 1
Next
Next
Else
For i = 1 To t1
For j = 1 To t2
arr(i, j) = k
k = k + 1
Next
Next
End If MsgBox "第" & arr(x1, y1) & "页,共" & imax & "页"
Application.ScreenUpdating = True
End Sub
|