简体   繁体   English

将范围设置为最后一行和打印区域末尾之间的空白空间 - Excel VBA PrintArea

[英]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.我试图弄清楚如何将范围设置为 LastRow +1 到该页面上 PrintArea 的末尾。

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?我找到了这个 function,但它似乎只是返回我输入的任何内容作为范围,我不确定它是否在做我想要的,有什么想法吗?

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.worksheet.hpagebreaks

https://docs.microsoft.com/en-us/office/vba/api/excel.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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