简体   繁体   中英

Set Range to Empty Space between Last Row and End of Print Area - Excel VBA PrintArea

I'm looking to add a bottom border to the last lines on a worksheet for notes. So it will print blank lines. I'm trying to figure out how to set the range to be the LastRow +1 to the End of the PrintArea on that page.

I found this function, but it just seems to return whatever I input as the range and I'm not sure it's doing what I want, any ideas?

Sub Test()
ActiveSheet.PageSetup.PrintArea = "A1:F10000"
Call PrintAreaLastRow
End Sub

Function PrintAreaLastRow() As Long
    With Range(ActiveSheet.PageSetup.PrintArea)
        PrintAreaLastRow = .Rows(.Rows.Count).Row
        Debug.Print PrintAreaLastRow
    End With
End Function

https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet.hpagebreaks

https://docs.microsoft.com/en-us/office/vba/api/excel.hpagebreaks

Test:

Sub Test()

Dim wb As Workbook, ws As Worksheet
Set wb = ActiveWorkbook
Set ws = ActiveSheet

Dim lPrintRow As Integer
lPrintRow = LastPrintRow(ws)
Debug.Print lPrintRow

Dim UnUsedRng As Range
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
lColLet = Split(Cells(1, lCol).Address, "$")(1)
lRow = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
Set UnUsedRng = ws.Range("A" & lRow & ":" & lColLet & lPrintRow)

UnUsedRng.Borders(xlBottom).LineStyle = xlContinuous
UnUsedRng.Borders(xlBottom).Weight = xlThin

End Sub

Func:

Public Function LastPrintRow(ByRef strSheet As Worksheet) As Integer

Dim RowsPerPage As Long, PrintPages As Long
pgBreak = strSheet.HPageBreaks(1).Location.Address(False, False)
RowsPerPage = Right(pgBreak, Len(pgBreak) - 1) - 1
PrintPages = strSheet.HPageBreaks.Count + 1
LastPrintRow = RowsPerPage * PrintPages - PrintPages + 1
Debug.Print LastPrintRow

End Function

If I understand your question correctly, this should work

Sub Test()
    Dim rngBlankLines As Range
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    ws.PageSetup.PrintArea = "A1:F10000"
    
    With ws.Range(ws.PageSetup.PrintArea)
       Set rngBlankLines = ws.Range(ws.Cells(GetPrintAreaLastUsedRow(ws) + 1, .Cells(1, 1).Column), _
                                    .Cells(.Rows.Count, .Columns.Count))
    End With
    
    With rngBlankLines
        ' Select to check if it is the range you expect (Delete if correct)
        .Select
        
'        ' Add your formatting below (uncomment if rngBlankLines is the correct range)
'        With .Borders(xlEdgeBottom)
'            .LineStyle = xlContinuous
'            .Weight = xlThin
'        End With
'
'        With .Borders(xlInsideHorizontal)
'            .LineStyle = xlContinuous
'            .Weight = xlThin
'        End With
'
'        ' Perhaps give more room for hand-writing
'        .EntireRow.RowHeight = .Cells(1, 1).Offset(-1, 0).EntireRow.RowHeight * 1.5
    End With
    
End Sub

Function GetPrintAreaLastUsedRow(ws As Worksheet) As Long
    Dim i As Long, lTemp As Long
    
    If ws.PageSetup.PrintArea = "" Then Exit Function
    
    With ws.Range(ws.PageSetup.PrintArea)
        For i = 1 To .Columns.Count
            lTemp = .Cells(.Rows.Count, i).End(xlUp).Row
            If GetPrintAreaLastUsedRow < lTemp Then GetPrintAreaLastUsedRow = lTemp
        Next i
    End With
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM