这是我一段时间以来一直在努力解决的问题。请原谅我没有引用我研究过的确切线索,但我没有保留日志。我下面的代码示例是通过审查有关该主题的许多线程构建的。然而,我仍然无法找到我需要的确切解决方案。
简而言之:在 Excel VBA 中,在动态创建和填充后,我需要能够识别长表中每页的最后一行和第一/最后一列,该表可能有 1 到 5 页长(水平)。我还希望有一种快速的方法来识别填充数据的停止位置,但如果没有简单的解决方案,可以从生成例程中获取该数据。以下代码将识别单个页表的正确最终行和列...一次。我可以进行必要的修改来检查多页表的后续页面。我遇到的真正问题是,一旦填充并清除了单元格,Excel 就会将该单元格包含在已用单元格范围中。同一代码的后续执行会失败,因为最后一个单元格不再被正确识别。有没有办法扭转这种情况,或者我可以采取不同的方法?
我确实不喜欢填充单元格并删除它们来查找分页符,但我还没有找到避免这样做的解决方案。感谢您提供的任何指导。,迈克·沙纳汉
Sub Findpagebreaks()
Dim x As HPageBreaks, pb_x As HPageBreak
Dim y As VPageBreaks, pb_y As VPageBreak, PageMatrix() As Integer
Dim LastPopulated(1) As Integer, test As Integer, target As Integer
ReDim PageMatrix(1 To 1, 0 To 2) As Integer
With ActiveSheet
Debug.Print "============================================"
Debug.Print "Horizontal Page Breaks"
Set x = .HPageBreaks
Debug.Print "Initial Hbreaks: ", x.Count
LastPopulated(0) = .Cells.SpecialCells(xlCellTypeLastCell).Row
Debug.Print "Last row of data: ", LastPopulated(0)
target = x.Count + 1
test = LastPopulated(0)
Do While x.Count < target
test = test + 10
.Cells(test, 1).Value = "."
Debug.Print "cell: " & .Cells(test, 1).Address & " populated"
If test > 100 Then Exit Do
Loop
For Each pb_x In x
If pb_x.Extent = xlPageBreakFull Then
Debug.Print "Row: " & pb_x.Location.Row, "Full Page Break"
PageMatrix(1, 2) = pb_x.Location.Row - 1
Else
Debug.Print "Row: " & pb_x.Location.Row, "Partial Page Break"
End If
Next pb_x
.Range(.Cells(LastPopulated(0) + 1, 1), .Cells(test, 1)).ClearContents
Debug.Print "cells: " & .Range(.Cells(LastPopulated(0), 1), .Cells(test, 1)).Address & " cleared."
Debug.Print "Horizontal Exploration complete."
Debug.Print "Vertical Page Breaks"
Set y = .VPageBreaks
Debug.Print "Initial vbreaks: ", y.Count
LastPopulated(1) = .Cells.SpecialCells(xlCellTypeLastCell).Column
Debug.Print "Last column of data: ", LastPopulated(1)
target = y.Count + 1
test = LastPopulated(1)
Do While y.Count < target
test = test + 10
.Cells(1, test).Value = "."
Debug.Print "cell: " & .Cells(1, test).Address & " populated"
If test > 100 Then Exit Do
Loop
PageMatrix(1, 0) = 1
For Each pb_y In y
If pb_y.Extent = xlPageBreakFull Then
Debug.Print "column: " & pb_y.Location.Column, "Full Page Break"
PageMatrix(1, 1) = pb_y.Location.Column - 1
Else
Debug.Print "Row: " & pb_y.Location.Column, "Partial Page Break"
End If
Next pb_y
.Range(.Cells(1, LastPopulated(1) + 1), .Cells(1, test)).ClearContents
Debug.Print "cells: " & .Range(.Cells(1, LastPopulated(1)), .Cells(1, test)).Address & " cleared."
Debug.Print "Vertical Exploration complete."
Debug.Print "Page", "First Col", "Last Col", "Last Row"
Debug.Print 1, PageMatrix(1, 0), PageMatrix(1, 1), PageMatrix(1, 2)
Debug.Print "~~~~~~~~~~~~~~~~~~~~~~"
Debug.Print "Sub complete."
Debug.Print
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
不是完整的答案,但大大减少了一些代码。
当然,似乎有很多代码只是为了查找分页符。对于分页符,请尝试这个简单的例程。根据您的情况进行修改。
Sub Sample()
'Horizontal Pagebreaks
For h = 1 To 100
If Sheets("Sheet1").Rows(h).PageBreak <> xlPageBreakNone Then MsgBox "Hor " & n
Next
'Vertical Pagebreaks
For v = 1 To 100
If Sheets("Sheet1").Columns(v).PageBreak <> xlPageBreakNone Then MsgBox "Ver " & n
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13015 次 |
| 最近记录: |