设置Excel文件的打印区域

我有一个Excel文件,其中所有的列直到列N 我的问题是我想知道如何设置打印区域使用VBScript覆盖每一行,直到列N直到一个空白行。

取决于您要使用哪个列来检查第一个空白行。 下面是一个例子,用于检查列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 

除非您的工作表在该空白行之后有更多的数据行,否则您可以简单地使用UsedRange属性:

 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