Hi all,
I am trying to find a way to identify which page a cell is on (after a print preview is performed) and then insert rows to the worksheet based on what values are found before and after the page break:
if values on page1 <> values on page2 then just add header:
1
1
1
1
----page break----
2
2
2
3
3
becomes:
1
1
1
1
----page break----
<insert row>
2
2
2
3
3
and if values page1 = values page2 then insert rows until they don't
1
1
1
1
2
2
-------page break-----
2
2
3
3
3
becomes
1
1
1
1
<insert row>
<insert row>
----page break----
<insert row>
2
2
2
2
3
3
3
The code I have so far is:
Private Sub setPageHeaders()
Dim lrow, x, page1, page2, i As Integer
Dim rowSelection As String
lrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lrow
page2 = #getPageNumber(ActiveSheet.cells(x, 1))# 'get the page number of current line
page1 = #getPageNumber(ActiveSheet.cells(x - 1, 1))# 'get the page number of the previous line
If page2 <> page1 Then 'compare page numbers until the do not match, indicating page break
If ActiveSheet.Cells(x, 1).Value = ActiveSheet.Cells(x - 1, 1).Value Then 'if values do not match, insert header row at top of page
ActiveSheet.Rows(x).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ElseIf ActiveSheet.Cells(x, 1).Value = ActiveSheet.Cells(x - 1, 1).Value Then 'find if values do match, find value that does not match and move down to next page & add header row
i = 0
Do Until ActiveSheet.Cells(x, 1).Value <> ActiveSheet.Cells(x - i, 1).Value 'when the do match, find last value on page1 that matches
i = i + 1
Loop
rowSelection = x - i & ":" & x 'insert rows so that there is a clean break between values
ActiveSheet.Rows(rowSelection).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
End If
Next x
where the two lines with the #getPageNumber()# variable standin is where I don't know the command for getting the page number.
Is there an easy way to get this?
Bookmarks