我在一个工作簿中有300个不同的工作表,并且它们都有不同的长度。我需要全部打印出来。我想使用VBA设置打印区域,直到使用范围的最后一行,然后打印它们,因为它们都不同。
在“ I”列和行变化之前,列是相同的
请帮助!
答案 0 :(得分:3)
我相信以下内容将帮助您实现所需的目标,它将遍历所有工作表,并在第一列上找到带有值的最后一行,然后将打印区域设置为这样:
Sub PrintArea()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
'loop through all sheets in workbook
LastRow = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row
'get the last row with data on Column I
ws.PageSetup.PrintArea = "A1:I" & LastRow
'set print area from A1 to last row on column I
Next ws
End Sub
答案 1 :(得分:1)
您可以循环浏览纸张并设置打印区域
Sub DoIt()
Dim sh As Worksheet
Dim LstRw As Long, Rng As Range
For Each sh In Sheets
With sh
LstRw = .Cells(.Rows.Count, "I").End(xlUp).Row
Set Rng = .Range("A1:I" & LstRw)
.PageSetup.PrintArea = Rng.Address
End With
Next sh
End Sub