Option Explicit
Sub Place_Fractions_On_Visible_Rows_To_Print()
'Assumes that the 1/A fractions are placed starting at row 2 or greater!!!
Dim printRange As String
Dim totalVisibleRowsToBePrintedSoFar As Integer
Dim firstRowOnNextPrintedPage As Long
Dim numOfVisRowsOnCurrentPrintedPage As Integer
Dim currentPageBreakRowNumber As Long
Dim topCellToPlaceFractions As String
Dim lastUsedColumnNumber As Integer
Dim firstRow As Long
Dim lastRow As Long
Dim lastusedRowInSheet As Long
Dim visibleRowIndicator_ColumnLetter As String
Dim numberOfPrintedRowsPerPage_ColumnLetter As String
Dim letterNumberOnCurrentPrintedPage_ColumnLetter As String
Dim columnToPut_Fractions_ColumnLetter As String
'___________________________________
'Input
topCellToPlaceFractions = "B7"
'___________________________________
firstRow = Range(topCellToPlaceFractions).Row
printRange = RangeSelectionPrompt("Select Print Range (Pres [Esc] to keep previous)")
With ActiveSheet
If printRange <> "" Then
.PageSetup.PrintArea = printRange
Else
printRange = .PageSetup.PrintArea
End If
lastRow = Range(printRange).Rows.Count + firstRow - 1
'Place the helper columns in the columns to the RIGHT of the right-most used column of the data.
lastUsedColumnNumber = .UsedRange.Columns.Count + .UsedRange.Column - 1 'The actual right-most column.
'****************************************************************
'Fill helper column that identifies rows with a row height >= 10.
'****************************************************************
lastUsedColumnNumber = lastUsedColumnNumber + 1 'a new column to the right of that.
visibleRowIndicator_ColumnLetter = Split(Cells(1, lastUsedColumnNumber).Address, "$")(1)
'=IF(@RowHeight(A7)>=10,H6+1,H6)
.Range(visibleRowIndicator_ColumnLetter & firstRow & ":" & visibleRowIndicator_ColumnLetter & lastRow).Formula = _
"=IF(RowHeight(M7)>=10," & visibleRowIndicator_ColumnLetter & firstRow - 1 & "+1," & visibleRowIndicator_ColumnLetter & firstRow - 1 & ")"
'*******************************************************************************************************
'Fill helper column that marks the VISIBLE row that is the last row on the current (to be) printed page.
'*******************************************************************************************************
lastUsedColumnNumber = lastUsedColumnNumber + 1 'a new column to the right of that.
numberOfPrintedRowsPerPage_ColumnLetter = Split(Cells(1, lastUsedColumnNumber).Address, "$")(1)
totalVisibleRowsToBePrintedSoFar = 0
For currentPageBreakRowNumber = 1 To .HPageBreaks.Count
firstRowOnNextPrintedPage = .HPageBreaks(currentPageBreakRowNumber).Location.Row
numOfVisRowsOnCurrentPrintedPage = Range(visibleRowIndicator_ColumnLetter & firstRowOnNextPrintedPage - 1).Value - totalVisibleRowsToBePrintedSoFar
.Range(numberOfPrintedRowsPerPage_ColumnLetter & firstRowOnNextPrintedPage - 1).Value = numOfVisRowsOnCurrentPrintedPage
totalVisibleRowsToBePrintedSoFar = totalVisibleRowsToBePrintedSoFar + numOfVisRowsOnCurrentPrintedPage
Next
'*****************************************************************************************
'Fill helper column that signifies the "increment" of the Column Letters in the fractions.
'*****************************************************************************************
lastUsedColumnNumber = lastUsedColumnNumber + 1 'a new column to the right of that.
letterNumberOnCurrentPrintedPage_ColumnLetter = Split(Cells(1, lastUsedColumnNumber).Address, "$")(1)
'=IF(I7<>"",J6+1,H7-SUM($I$7:I7))
.Range(letterNumberOnCurrentPrintedPage_ColumnLetter & firstRow & ":" & letterNumberOnCurrentPrintedPage_ColumnLetter & lastRow).Formula = _
"=IF(" & numberOfPrintedRowsPerPage_ColumnLetter & firstRow & "<>" & Chr(34) & Chr(34) & _
"," & letterNumberOnCurrentPrintedPage_ColumnLetter & firstRow - 1 & "+1," & visibleRowIndicator_ColumnLetter _
& firstRow & "-SUM($" & numberOfPrintedRowsPerPage_ColumnLetter & "$" & firstRow & ":" & numberOfPrintedRowsPerPage_ColumnLetter & firstRow & "))"
'*********************************************************************
'Fill the column specified by user with the #/column letter fractions.
'*********************************************************************
lastusedRowInSheet = .UsedRange.Rows.Count + .UsedRange.Row - 1
columnToPut_Fractions_ColumnLetter = Split(Cells(1, Range(topCellToPlaceFractions).Column).Address, "$")(1)
'Clear previous contents in the column of focus (even beneath the print range if the print range doesn't go all the way to the end).
.Range(columnToPut_Fractions_ColumnLetter & firstRow & ":" & columnToPut_Fractions_ColumnLetter & lastusedRowInSheet).Formula = ""
With .Range(columnToPut_Fractions_ColumnLetter & firstRow & ":" & columnToPut_Fractions_ColumnLetter & lastRow)
.Formula = ""
'=SUM(IF($I$6:I6<>"",1,0))+1&"/"&SUBSTITUTE(SUBSTITUTE(ADDRESS(1,J7),"$",""),"1","")
.Formula = _
"=SUM(IF(" & "$" & numberOfPrintedRowsPerPage_ColumnLetter & "$" & firstRow - 1 & ":" & numberOfPrintedRowsPerPage_ColumnLetter & firstRow - 1 & "<>" _
& Chr(34) & Chr(34) & ",1,0))+1&" & Chr(34) & "/" & Chr(34) & "&SUBSTITUTE(SUBSTITUTE(ADDRESS(1," & letterNumberOnCurrentPrintedPage_ColumnLetter _
& firstRow & ")," & Chr(34) & "$" & Chr(34) & "," & Chr(34) & Chr(34) & ")," & Chr(34) & "1" & Chr(34) & "," & Chr(34) & Chr(34) & ")"
.Replace What:="@", Replacement:="", LookAt:=xlPart, FormulaVersion:=xlReplaceFormula2
ActiveSheet.Calculate
.Formula = .Value 'Line #1
End With
'***********************************************
'Clear the formulas in all three helper columns.
'***********************************************
.Range(visibleRowIndicator_ColumnLetter & firstRow & ":" & letterNumberOnCurrentPrintedPage_ColumnLetter & lastRow).Value = "" 'Line #2
End With
End Sub
Function RowHeight(MR As Range) As Double
RowHeight = MR.RowHeight
End Function
Sub Test__RangeSelectionPrompt()
MsgBox RangeSelectionPrompt("Choose Cells")
End Sub
Function RangeSelectionPrompt(titleOfRangeSelectionPromptBox As String)
'Code is from http://www.vbaexpress.com/forum/showthread.php?763-Solved-Inputbox-Cell-Range-selection-Nothing-selected-or-Cancel&p=6680&viewfull=1#post6680
Dim Selectedarea As Range
On Error Resume Next
Set Selectedarea = Application.InputBox(prompt:="Left click on the top-left cell and drag to the botSomething-right cell.", _
Title:=titleOfRangeSelectionPromptBox, Default:=Selection.Address, Type:=8)
'If the user clicked on cancel,
If Selectedarea Is Nothing Then Exit Function
RangeSelectionPrompt = Selectedarea.Address
End Function