我正在嘗試獲取一個代碼,該代碼將圍繞特定范圍的單元格(所選單元格 - 因為它必須是動態的)自動設定列印視圖模式并洗掉頁碼(黑色的第 1 頁疊加在單元格上)。
我嘗試了以下(如下),但是它不起作用,因為它抓取所有作業表資料并將其放在一頁上,而不是僅在一頁上顯示所選范圍。
Dim myRange As Range
Set myRange = Selection
ActiveWindow.View = xlPageBreakPreview
myRange.Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.708661417322835)
.RightMargin = Application.InchesToPoints(0.708661417322835)
.TopMargin = Application.InchesToPoints(0.748031496062992)
.BottomMargin = Application.InchesToPoints(0.748031496062992)
.HeaderMargin = Application.InchesToPoints(0.31496062992126)
.FooterMargin = Application.InchesToPoints(0.31496062992126)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
End Sub
所以基本上我希望選定的單元格突出顯示(白色)并且只放在一頁上(沒有分頁符),并且作業表中的任何其他東西都放在列印視圖之外(在那個灰色/黑色視圖中)。
任何想法都會受到歡迎。
謝謝 !
uj5u.com熱心網友回復:
錄制宏時要記住的一件事是,通常不需要提及默認設定,除非您要更改數字。
因此.LeftHeader = "",除非您專門從左標題中洗掉文本,否則不需要諸如此類的東西- 默認情況下它是空白的,而這段代碼只是說保持空白。
疊加在作業表上的頁碼只是因為您在分頁預覽中(您的代碼將您放在開頭)。如果您手動更改列印,您只需要查看該視圖 - 使用代碼只需停留在普通視圖中(很少需要在使用 VBA 之前選擇任何內容)。
這段代碼對我有用:
Sub Test()
Dim PrintRange As Range
Set PrintRange = Selection
With PrintRange.Parent.PageSetup 'The Parent of Selection is the sheet.
Application.PrintCommunication = True
.PrintArea = PrintRange.Address
Application.PrintCommunication = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/392490.html
上一篇:如何使用VBA從特定的Outlook檔案夾中獲取電子郵件
下一篇:根據選擇指定的單元格顯示形狀
