Macro Excel sẽ không thay đổi thông tin tiêu đề

Macro Excel sẽ không thay đổi thông tin tiêu đề

Tôi đang viết một macro trong Excel 2010 để in thông tin trang. Tôi đã thiết lập 2 macro, một macro thay đổi tiêu đề trên trang in thành "Hóa đơn" và một macro thay đổi tiêu đề thành "Bản sao Office". Lần đầu tiên tôi chạy macro này, nó đã thay đổi tiêu đề đúng cách (Ví dụ: nếu tôi chạy macro "Hóa đơn"). Nhưng khi tôi chạy macro thứ hai, tiêu đề vẫn còn thông tin từ macro đầu tiên (Vẫn là "Hóa đơn", đáng lẽ phải là "Bản sao Office"). Nhưng nếu tôi vào thiết lập trang và xóa hết thông tin tiêu đề, rồi chạy macro thứ hai, tiêu đề sẽ hiển thị thông tin chính xác ("Bản sao Office"). Sau khoảng một hoặc hai giờ mày mò, về cơ bản, chức năng của macro này là nếu thông tin Tiêu đề trống, nó sẽ đưa thông tin cần thiết vào tiêu đề. Nhưng nếu tiêu đề đã có thông tin, nó sẽ không thay thế thông tin đó.

Tôi không thiết lập bất kỳ chế độ bảo vệ bảng tính hay sổ làm việc nào và không thể tìm thấy bất kỳ cài đặt nào hạn chế loại thay đổi này.

Vậy đây có phải là lỗi trong chương trình không? Có thiết lập nào tôi bỏ sót mà tôi có thể thay đổi để các macro này hoạt động không, hay có cách diễn đạt khác nhau trong Visual Basic cho macro mà tôi cần thay đổi không?

Đây là đoạn mã nếu nó hữu ích. 2 dòng mà tôi cảm thấy là vấn đề, tôi đã in đậm, in nghiêng và gạch chân.

Sub PrintPreviewInvoice()
'
' PrintPreviewInvoice Macro
' Tạo ngày 15/7/2010. Xem trước bản in của vùng Hóa đơn: phạm vi E2:K23.
'
' Phím tắt: Ctrl+p
'
Phạm vi("E2:K23").Chọn
ActiveSheet.PageSetup.PrintArea = "$E$2:$K$23"
Application.PrintCommunication = False
Với ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
Kết thúc bằng
Application.PrintCommunication = Đúng
ActiveSheet.PageSetup.PrintArea = "$E$2:$K$23"
Application.PrintCommunication = False
Với ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "HÓA ĐƠN"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0,75)
.RightMargin = Application.InchesToPoints(0,75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0,5)
.FooterMargin = Application.InchesToPoints(0,5)
.PrintHeadings = Sai
.PrintGridlines = Sai
.PrintComments = xlPrintSheetEnd
.CenterHorizontally = Đúng
.CenterVertically = Sai
.Định hướng = xlPortrait
.Draft = Sai
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = Sai
.Phóng to = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = Sai
.DifferentFirstPageHeaderFooter = Sai
.ScaleWithDocHeaderFooter = Đúng
.AlignMarginsHeaderFooter = Sai
.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 = ""
Kết thúc bằng
Application.PrintCommunication = Đúng
Phạm vi("B3").Chọn
Kết thúc phụ đề
Sub PrintPreviewEntireSheet()
'
' Macro PrintPreviewToàn bộ trang tính
' Tạo ngày 15/7/2010. Xem trước khi in toàn bộ vùng bảng tính: phạm vi A1 K25
'
' Phím tắt: Ctrl+s
'
Phạm vi("A1:K25").Chọn
ActiveSheet.PageSetup.PrintArea = "$A$1:$K$25"
Application.PrintCommunication = False
Với ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
Kết thúc bằng
Application.PrintCommunication = Đúng
ActiveSheet.PageSetup.PrintArea = "$A$1:$K$25"
Application.PrintCommunication = False
Với ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "BẢN SAO CHÍNH THỨC"
.RightHeader = ""
.LeftFooter = "Ellen Jefferson, Giám đốc kinh doanh"
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0,75)
.RightMargin = Application.InchesToPoints(0,75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0,5)
.FooterMargin = Application.InchesToPoints(0,5)
.PrintHeadings = Sai
.PrintGridlines = Sai
.PrintComments = xlPrintSheetEnd
.CenterHorizontally = Đúng
.CenterVertically = Sai
.Định hướng = xlPortrait
.Draft = Sai
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = Sai
.Zoom = Sai
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = Sai
.DifferentFirstPageHeaderFooter = Sai
.ScaleWithDocHeaderFooter = Đúng
.AlignMarginsHeaderFooter = Sai
.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 = ""
Kết thúc bằng
Application.PrintCommunication = Đúng
Phạm vi("B3").Chọn
Kết thúc phụ đề




Trả lời:

RD, có ổn không nếu bạn xóa (thử chỉ cần chú thích trước) các dòng Application.PrintCommunication?


Trân trọng, Tom

Trả lời:

Ồ, tôi sẽ là một thằng con hoang. Nó làm mọi thứ chậm lại đáng kể, nhưng nó hiệu quả! Cảm ơn, cảm ơn rất nhiều.

Trân trọng, Robert



Trả lời:

Cảm ơn Robert đã phản hồi.

Nếu mã của bạn được tạo bằng trình ghi macro, bạn có thể thấy một số dòng mã không thực sự cần thiết - hãy thử xóa (ghi chú các dòng), thường là những dòng tham chiếu đến giá trị mặc định. (Xem thêm tại đây: http://stackoverflow.com/questions/230382/how-to-print-faster-in-excel-vba )

Ví dụ, có thể những điều sau đây sẽ phù hợp với mục đăng ký đầu tiên của bạn:

Sub PrintPreviewInvoice()
'
' PrintPreviewInvoice Macro
' Tạo ngày 15/7/2010. Xem trước bản in của vùng Hóa đơn: phạm vi E2:K23.
'
' Phím tắt: Ctrl+p
'
ActiveSheet.PageSetup.PrintArea = "$E$2:$K$23"
Với ActiveSheet.PageSetup
.CenterHeader = "HÓA ĐƠN"
.LeftMargin = Application.InchesToPoints(0,75)
.RightMargin = Application.InchesToPoints(0,75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0,5)
.FooterMargin = Application.InchesToPoints(0,5)
.PrintHeadings = Sai
.PrintGridlines = Sai
.PrintComments = xlPrintSheetEnd
.CenterHorizontally = Đúng
.CenterVertically = Sai
.Định hướng = xlPortrait
.Draft = Sai
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = Sai
.Phóng to = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = Sai
.DifferentFirstPageHeaderFooter = Sai
.ScaleWithDocHeaderFooter = Đúng
.AlignMarginsHeaderFooter = Sai
Kết thúc bằng
Phạm vi("B3").Chọn
Kết thúc phụ đề

Để tăng tốc độ thực thi, hãy thử thêm những dòng sau trước dòng PrintArea:

Dim origCalcMode As XlCalculation
Làm mờ sActivePrinter dưới dạng chuỗi
'
origCalcMode = Ứng dụng.Tính toán
sActivePrinter = Ứng dụng.ActivePrinter
'
Ứng dụng.ScreenUpdating = Sai
Ứng dụng.Tính toán = xlCalculationManual
Application.ActivePrinter = "Microsoft XPS Document Writer trên Ne01:"

và những dòng này trước dòng Range("B3").Select:

Application.ScreenUpdating = Đúng
Ứng dụng.Tính toán = origCalcMode
Ứng dụng.ActivePrinter = sActivePrinter

(Xem chủ đề này: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/8b2a0988-7d09-4abf-a4d3-76e81f708d40 )


Trân trọng, Tom

Comments

Popular posts from this blog

Khẩn cấp - File Excel chứa các ký tự đặc biệt ở họ và tên liên hệ

Excel 2016 - mở tất cả các tệp trong MỘT phiên bản

Điều tôi muốn làm trong Excel 2010 là tạo một nút tùy chỉnh và gắn nó vào thanh công cụ Truy nhập nhanh và chạy một macro cụ thể.