我已经尝试了下面的两个。这两种方法都工作得很好,只是它会导致页面只包含标题,因为所选列中的值从标题值更改为第一个实际值。
选项1:
Dim I As Long, J As Long
J = ws.Cells(Rows.Count, "b").End(xlUp).Row
For I = J To 2 Step -1
If Range("b" & I).Value <> Range("b" & I - 1).Value Then
ActiveSheet.HPageBreaks.Add Before:=Range("b" & I)
End If
Next I
选项2:
Dim rangeSelection As Range
Dim cellCurrent As Range
Set rangeSelection = Application.Selection.Columns(2).Cells
ActiveSheet.ResetAllPageBreaks
For Each cellCurrent In rangeSelection
If (cellCurrent.Row > 2) Then
If (cellCurrent.Value <> cellCurrent.Offset(-1, 0).Value) Then
ActiveSheet.Rows(cellCurrent.Row).PageBreak = _
xlPageBreakManual
End If
End If
Next cellCurrent
发布于 2020-10-02 10:35:15
选项1(假设已经设置了ws
)
Dim I As Long, J As Long
J = ws.Cells(ws.Rows.Count, "b").End(xlUp).Row
For I = J To 2 Step -1
If ws.Range("b" & I).Value <> ws.Range("b" & I - 1).Value Then
ws.HPageBreaks.Add Before:=ws.Range("b" & I)
End If
Next I
选项2(假设ws
设置为选项1中的值)
Dim rangeSelection As Range
Dim cellCurrent As Range
Set rangeSelection = ws.Range(ws.Cells(1, 2), ws.Cells(ws.Rows.Count, 2).End(xlUp))
ws.ResetAllPageBreaks
For Each cellCurrent In rangeSelection
If cellCurrent.Row > 2 Then
If cellCurrent.Value <> cellCurrent.Offset(-1, 0).Value Then
ws.Rows(cellCurrent.Row).PageBreak = xlPageBreakManual
End If
End If
Next cellCurrent
https://stackoverflow.com/questions/64164683
复制相似问题