應(yīng)用場(chǎng)景 設(shè)置僅僅打印活動(dòng)單元格所在頁(yè) 知識(shí)要點(diǎn) 1:HPageBreaks.Count 打印區(qū)域內(nèi)水平分頁(yè)符的集合的數(shù)量 2:VPageBreaks.Count 打印區(qū)域內(nèi)垂直分頁(yè)符的集合的數(shù)量 3:HPageBreak.Location 屬性 返回或設(shè)置定義分頁(yè)符位置的單元格,,水平分頁(yè)符與定位單元格的上邊緣對(duì)齊,;垂直分頁(yè)符與定位單元格的左邊緣對(duì)齊 4:PageSetup.Order 屬性 返回或設(shè)置一個(gè) XlOrder 值,它代表 Microsoft Excel 打印一張大工作表時(shí)所使用的頁(yè)編號(hào)的次序 Sub 打印當(dāng)前頁(yè)() '如果當(dāng)前表是空表,,則退出程序 If IsEmpty(ActiveSheet.UsedRange) Then MsgBox '當(dāng)前工作表為空,!', 64, '出錯(cuò)': Exit Sub If Intersect(ActiveSheet.UsedRange, ActiveCell) Is Nothing Then MsgBox '請(qǐng)選擇已用區(qū)域!', 64, '出錯(cuò)': Exit Sub Dim 水平分頁(yè)符 As Integer, 垂直分頁(yè)符 As Integer, hrow As Integer Dim 當(dāng)前頁(yè) As Integer, m As Integer, n As Integer, vcolumn ActiveWindow.View = xlPageBreakPreview 水平分頁(yè)符 = ActiveSheet.HPageBreaks.Count '記錄水平分頁(yè)符數(shù)量 垂直分頁(yè)符 = ActiveSheet.VPageBreaks.Count '記錄垂直分頁(yè)符數(shù)量 '獲取當(dāng)前頁(yè)的橫向位置 For n = 1 To 水平分頁(yè)符 '橫向循環(huán) hrow = ActiveSheet.HPageBreaks(n).Location.Row '返回分頁(yè)符的單元格行號(hào) If hrow > ActiveCell.Row Then Exit For '如果大于當(dāng)前行號(hào) 則退出循環(huán) Next '獲取當(dāng)前頁(yè)的縱向位置 For m = 1 To 垂直分頁(yè)符 vcolumn = ActiveSheet.VPageBreaks(m).Location.Column '返回分頁(yè)符位置的單元格列號(hào) If vcolumn > ActiveCell.Column Then Exit For '如果大于當(dāng)前列號(hào)則退出循環(huán) Next '如果先橫向后縱向打印 If ActiveSheet.PageSetup.Order = xlOverThenDown Then 當(dāng)前頁(yè) = (n - 1) * (垂直分頁(yè)符 1) m '記錄當(dāng)前的頁(yè)數(shù) Else 當(dāng)前頁(yè) = (m - 1) * (水平分頁(yè)符 1) n '記錄當(dāng)前頁(yè)數(shù) End If If MsgBox('當(dāng)前單元格在本工作表中位于' & Chr(10) & '第' & 當(dāng)前頁(yè) & '頁(yè)' & Chr(10) & '是否打印當(dāng)前頁(yè)', 65, '友情提示') = vbOK Then ActiveSheet.PrintOut from:=當(dāng)前頁(yè), To:=當(dāng)前頁(yè) End If ActiveWindow.View = xlNormalView End Sub |
|