简体   繁体   中英

Set Print Area for Excel Files

I have an Excel file all of which has columns until col N . My question is I want to know how to set the print area using VBScript to cover each row until column N until a blank row.

Depends on which column you want to use to check for the first blank row. Here's an example that checks for the last row with data in column A:

Const xlUp = -4162
Const xlCellTypeBlanks = 4

' Get the last row...
Dim intLastRow
intLastRow = objWSheet.Cells(objWSheet.Rows.Count, 1).End(xlUp).Row

' Delete any rows with a blank column A...
objWSheet.Range("A1:A" & intLastRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

' Get the new last row...
intLastRow = objWSheet.Cells(objWSheet.Rows.Count, 1).End(xlUp).Row

' Set the print area...
objWSheet.PageSetup.PrintArea = "$A$1:$N$" & intLastRow

Unless your sheet has more data rows after that blank row you could simply use the UsedRange property:

Set xl = CreateObject("Excel.Application")
xl.Visible = True

Set wb = xl.Workbooks.Open("C:\path\to\your.xlsx")
Set ws = wb.Sheets(1)

ws.PageSetup.PrintArea = ws.UsedRange.Address

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