|
发表于 2024-7-5 16:26
来自手机
|
显示全部楼层
本帖最后由 lss001 于 2024-7-5 20:57 编辑
510621666 发表于 2024-7-5 14:08
到我的工作簿用不了
'表格包含隐藏单元格区域如何添加水平与垂直分页符?
'************************************
Sub 添加水平与垂直分页符并打印()
Dim i&, j&, x&, y&, hp%, vp%, r As Range, c As Range
With Sheet1 '指定打印表格
'设置水平左对齐,垂直上对齐
.UsedRange.HorizontalAlignment = xlLeft
.UsedRange.VerticalAlignment = xlTop
x = .Cells(Rows.Count, 1).End(3).Row '获取最大行列
y = .Cells(1, Columns.Count).End(1).Column
Set Rng = .Range(.Cells(1, 1), .Cells(x, y))
.ResetAllPageBreaks '重置所有分页符
i = 0 '初始化i
hp = 26 '指定水平分页符行数:25+1
For Each r In Rng
'判断是否隐藏行
If r.EntireRow.Hidden = False Then
i = i + 1
'判断是否等于指定水平分页符:行数
If i = hp Then
'添加水平分页符
.HPageBreaks.Add r
hp = hp + 25
End If
End If
Next
j = 0 '初始化j
vp = 11 '指定垂直分页符列数:10+1
For Each c In Rng
'判断是否隐藏列
If c.EntireColumn.Hidden = False Then
j = j + 1
'判断是否等于指定垂直分页符:列数
If j = vp Then
'添加垂直分页符
.VPageBreaks.Add c
vp = vp + 10
End If
End If
Next
.PrintOut '打印
End With
End Sub |
|