@All
Hi!
As an exercise I keep on playing with the workbook which now has 4 worksheets:
- 1st sheet, Original, is the original data
- 2nd sheet, Reference, is a helper worksheet with cells containing row & col references
- 3rd sheet, PageBreak, is where all the things happen after running macro, ending with a table with row/column of page breaks
- 4th sheet, PrintedAtPage, is the result, where each cell has the value of the page at which it'd be printed
Named ranges in worksheet PageBreak:
RowList: =DESREF(PageBreak!$A$2;0;0;CONTARA(PageBreak!$A:$A);1) -----> in english: =OFFSET(PageBreak!$A$2,0,0,COUNTA(PageBreak!$A:$A),1)
ColList: =DESREF(PageBreak!$B$2;0;0;CONTARA(PageBreak!$B:$B);1) -----> in english: =OFFSET(PageBreak!$B$2,0,0,COUNTA(PageBreak!$B:$B),1)
PrintOrderCell: =$H$1
RowColTable: =DESREF(PageBreak!$F$2;0;0;CONTARA(PageBreak!$A:$A)-1;CONTARA(PageBreak!$B:$B)-1) -----> in english: =OFFSET(PageBreak!$F$2,0,0,COUNTA(PageBreak!$A:$A)-1,COUNTA(PageBreak!$B:$B)-1)
RowListIndex: =DESREF(RowList;-1;;FILAS(RowList)+1,) -----> in english: =OFFSET(RowList,-1,ROWS(RowList)+1,)
ColListIndex: =DESREF(ColList;-1;;FILAS(ColList)+1,) -----> in english: =OFFSET(ColList,-1,ROWS(ColList)+1,)
The only code is:
-----
Code:
Option Explicit
Sub GetPrintPageNumber()
'
' constants
Const ksWSOriginal = "Original"
Const ksWSInput = "Reference"
Const ksWSBreak = "PageBreak"
Const ksRow = "RowList"
Const ksCol = "ColList"
Const ksRowCol = "RowColTable"
Const ksPrintOrder = "PrintOrderCell"
Const ksWSPrinted = "PrintedAtPage"
'
' declarations
Dim rngRow As Range, rngCol As Range, rngRC As Range, rngOrder As Range
Dim I As Integer, J As Integer, K As Integer, L As Integer, M As Integer
Dim iOrder As Integer, bEndR As Boolean, bEndC As Boolean
'
' start
' ranges
Set rngRow = Worksheets(ksWSBreak).Range(ksRow)
Set rngCol = Worksheets(ksWSBreak).Range(ksCol)
Set rngRC = Worksheets(ksWSBreak).Range(ksRowCol)
Set rngOrder = Worksheets(ksWSBreak).Range(ksPrintOrder)
'
' process
With Worksheets(ksWSInput)
' print order
rngOrder.Cells(1, 1).Value = .PageSetup.Order
iOrder = .PageSetup.Order
' row breaks
rngRow.ClearContents
For I = 1 To .HPageBreaks.Count
rngRow.Cells(I).Value = .HPageBreaks(I).Location.Row - 1
Next I
rngRow.Cells(I).Value = .Rows.Count
' col breaks
For I = 1 To .VPageBreaks.Count
rngCol.Cells(I).Value = .VPageBreaks(I).Location.Column - 1
Next I
rngCol.Cells(I).Value = .Columns.Count
End With
With rngRC
.ClearContents
Select Case iOrder
Case xlDownThenOver
K = .Columns.Count
L = .Rows.Count
Case xlOverThenDown
K = .Rows.Count
L = .Columns.Count
End Select
M = 0
bEndR = False
For I = 1 To K
If iOrder = xlOverThenDown And rngRow.Cells(I, 1).Value = "" Or _
iOrder = xlDownThenOver And rngCol.Cells(I, 1).Value = "" Then
bEndR = True
Else
bEndC = False
For J = 1 To L
If iOrder = xlDownThenOver And rngRow.Cells(J, 1).Value = "" Or _
iOrder = xlOverThenDown And rngCol.Cells(J, 1).Value = "" Then
bEndC = True
Else
M = M + 1
Select Case iOrder
Case xlOverThenDown
.Cells(I, J).Value = M
Case xlDownThenOver
.Cells(J, I).Value = M
End Select
End If
If bEndC Then Exit For
Next J
End If
If bEndR Then Exit For
Next I
End With
'
' end
' ranges
Set rngOrder = Nothing
Set rngRC = Nothing
Set rngCol = Nothing
Set rngRow = Nothing
' beep
Beep
'
End Sub
-----
Formulas:
a) 2nd sheet, Reference, for all used cells in 1st sheet Original:
="row."&FILA()&"_col."&COLUMNA() -----> in english: ="row."&ROW()&"_col."&COLUMN()
b) 3rd sheet, PageBreak:
Column A: horizontal page breaks (filled by macro)
Column B: vertical page breaks (filled by macro)
Cell C1: Original and Reference page print order, down/over or over/down (filled by macro)
Cell D1: max printed pages (Horizontal page breaks + 1) * (Vertical page breaks + 1)
=CONTAR(RowList)*CONTAR(ColList) -----> in english: =COUNT(RowList)*COUNT(ColList)
Column E, E2:Exx : RowBreak
=SI.ERROR(INDICE(RowList;FILA()-FILA($E1));"") -----> in english: =IFERROR(INDEX(RowList,ROW()-ROW($E1)),"")
Row 1, F1:yy1 : ColBreak (last column before page break)
=SI.ERROR(INDICE(ColList;COLUMNA()-COLUMNA($E1));"") ----->in english: =IFERROR(INDEX(ColList,COLUMN()-COLUMN($J1)),"")
Range F2:yyxx : RowColBreak (double entry table with print page for cells up to row & col) (filled by macro)
Link to the file (same previous link):
https://dl.dropboxusercontent.com/u... a named cell (for qaliq at chandoo.org).xlsm
If any interested, just drop a few lines.
Regards!
EDITED