2017-09-10 68 views
0

這是我一直在摔跤一段時間的問題。原諒我不引用我研究過的確切線索,但我沒有保留日誌。下面的代碼示例是通過查看有關此主題的許多主題構建的。但是,我需要的確切解決方案仍然是逃避我。如何識別excel中的分頁符與VBA,變化條件

簡而言之:在Excel VBA中,我需要能夠識別長表中每頁的最後一行和第一/最後一列,長表中可能有1到5頁長(水平)創建和填充。我還希望能夠快速確定填充數據停止的位置,但如果沒有簡單的解決方案,可以從生成例程中獲取。以下代碼將識別單個頁表的正確最終行和列......一次。我可以進行修改以檢查多頁表格的後續頁面。我遇到的真正問題是,一旦一個單元格被填充並清除,excel將包含所使用單元格範圍內的單元格。隨後執行此相同的代碼失敗,因爲最後一個單元不再被正確識別。無論如何要扭轉這一點,或者我可以採取不同的方法?

我真的不喜歡填充單元格並刪除它們來查找分頁符,但我還沒有找到避免這樣做的解決方案。感謝您提供的任何指導。 ,Mike Shanahan

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 
+1

xlCellTypeLastCell不是你想要使用的。看看GetLastCell函數[here](http://www.cpearson.com/excel/LastCell.aspx) –

+0

謝謝Yan F.我會研究這個函數。 –

回答

0

所以我選擇了減少的編碼,使一個更強大的產品解決方案。這不是我尋找的複雜的答案,仍然屬於暴力方法論,但它確實不那麼繁重。

這是我現在最好的。如果有人發現更優雅的解決方案,請隨時發佈!謝謝大家。

Sub Findpagebreaks() 

Dim y As VPageBreaks, pb_y As VPageBreak, PageMatrix() As Integer 
Dim LastPopulated(1) As Integer, roww As Integer, coll As Integer 
Dim lastpage As Integer, LookingForLastPage As Boolean, last_row As Integer 

ReDim PageMatrix(0 To 2, 1 To 1) As Integer 
PageMatrix(0, 1) = 1 'First column of page 1, always = 1 
LookingForLastPage = True 
lastpage = 1 

With ActiveSheet 
    ' In practice, this sub will be passed values for row and coll 
    ' which represent the anticipated row & col that are populated 
    ' to span the used range. 
    roww = 13 
    coll = 1 
    LastPopulated(0) = .Cells(.Rows.Count, coll).End(xlUp).Row 
    LastPopulated(1) = .Cells(roww, .Columns.Count).End(xlToLeft).Column 
    Debug.Print "Last row of data: ", LastPopulated(0) 
    Debug.Print "Last column of data: ", LastPopulated(1) 
    .Cells(LastPopulated(0) + 50, LastPopulated(1) + 10).Value = "." 
    last_row = .HPageBreaks(1).Location.Row - 1 
    Set y = .VPageBreaks 
    For Each pb_y In y 
     If LookingForLastPage Then 
      If lastpage > 1 Then 
       ReDim Preserve PageMatrix(0 To 2, 1 To lastpage) As Integer 
       PageMatrix(0, lastpage) = PageMatrix(1, lastpage - 1) + 1 
      End If 
      PageMatrix(1, lastpage) = pb_y.Location.Column - 1 
      PageMatrix(2, lastpage) = last_row 
      If pb_y.Location.Column > LastPopulated(1) Then _ 
       LookingForLastPage = False Else: lastpage = lastpage + 1 
     End If 
    Next pb_y 
    .Cells(LastPopulated(0) + 50, LastPopulated(1) + 10).ClearContents 
    Debug.Print "Page", "First Col", "Last Col", "Last Row" 
    For i = 1 To lastpage 
     Debug.Print i, PageMatrix(0, i), PageMatrix(1, i), PageMatrix(2, i) 
    Next i 
    Debug.Print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 
    Debug.Print "Last page is: ", lastpage 

End With 
End Sub 
1

不是一個完整的答案,但大大減少了一些代碼。

當然好像很多代碼只是爲了尋找分頁符。對於分頁符,試試這個簡單的例程。修改您的情況。

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 
+0

謝謝米奇。不幸的是,這隻適用於已建立的分頁符。當數據尚未越過時(即.VPageBreaks.Count = 0),我需要能夠找到分頁符。沒關係,如果不那麼成熟的話,我會拋出毛巾並選擇更簡單的方法。 –

1
Sub List_VPageBreaks() 
Set f = ThisWorkbook.Worksheets("Sheet2") 
For i = 1 To f.VPageBreaks.Count 
Worksheets("Sheet3").Cells(i, 1).value=f.VPageBreaks(i).Location.Column 
Next 
End Sub 
+0

謝謝Docmarti。不幸的是,這隻適用於已建立的分頁符。當數據尚未越過時(即.VPageBreaks.Count = 0),我需要能夠找到分頁符。沒關係,如果不那麼成熟的話,我會拋出毛巾並選擇更簡單的方法。 –