Я записал макрос для настройки страницы, и он генерировал целый набор свойств, которые были установлены.Затем я запустил указанный макрос, попал на некоторые переписки, пошел в уборную, взял кофе, сел обратно и стал ждать окончания макроса подкачки.Само собой разумеется, что он невероятно медленен в обработке.
Я провел поиск в Google и обнаружил, что он работает очень медленно, и натолкнулся на три предложения.
- использование Встроенный в VBA 4Макрос для выполнения большинства ваших свойств набора страниц, поскольку он работает быстрее.
- ограничивает изменяемые вами свойства
- изменяет только те свойства, которые нужно изменить
3 - это добропохож на 2, и это был маршрут, который я решил взять.По сути, я бы запускал каждое изменение свойства через оператор if.
IF .property <> wanted.value then .property = wanted.value
Таким образом, с помощью небольшого количества текстовых манипуляций я быстро преобразовал записанный макрос, где он установил все значения свойств для проверки IF THEN, прежде чем устанавливать соответствующие значения.
Когда я запустил код, я получил ошибку «Несоответствие типов».Это не привело меня к какой-либо конкретной линии.Однако, когда я прошел через код, он вылетел в следующей строке:
If .PrintQuality <> 600 Then .PrintQuality = 600
Строка без IF работает без ошибок
.PrintQuality = 600
Код IF IF THEN работал на несколькихлинии, продолжающие этот. Что не так с проверкой PRINTQUALITY, которая выдает ошибку?
SAMPLE CODE
Sub SetupPage(ByVal wks As Worksheet)
Select Case wks.Name
Case Worksheets(2).Name
'Set Page size margins etc
With wks.PageSetup
If .PrintTitleRows <> "$1:$12" Then .PrintTitleRows = "$1:$12"
If .PrintTitleColumns <> "" Then .PrintTitleColumns = ""
If .LeftHeader <> "" Then .LeftHeader = ""
If .CenterHeader <> "" Then .CenterHeader = ""
If .RightHeader <> "" Then .RightHeader = ""
If .LeftFooter <> "" Then .LeftFooter = ""
If .CenterFooter <> "Page &P of &N" Then .CenterFooter = "Page &P of &N"
If .RightFooter <> "" Then .RightFooter = ""
If .LeftMargin <> Application.InchesToPoints(0.236220472440945) Then .LeftMargin = Application.InchesToPoints(0.236220472440945)
If .RightMargin <> Application.InchesToPoints(0.236220472440945) Then .RightMargin = Application.InchesToPoints(0.236220472440945)
If .TopMargin <> Application.InchesToPoints(0.748031496062992) Then .TopMargin = Application.InchesToPoints(0.748031496062992)
If .BottomMargin <> Application.InchesToPoints(0.748031496062992) Then .BottomMargin = Application.InchesToPoints(0.748031496062992)
If .HeaderMargin <> Application.InchesToPoints(0.31496062992126) Then .HeaderMargin = Application.InchesToPoints(0.31496062992126)
If .FooterMargin <> Application.InchesToPoints(0.31496062992126) Then .FooterMargin = Application.InchesToPoints(0.31496062992126)
If .PrintHeadings <> False Then .PrintHeadings = False
If .PrintGridlines <> False Then .PrintGridlines = False
If .PrintComments <> xlPrintNoComments Then .PrintComments = xlPrintNoComments
****************************************************************************
If .PrintQuality <> 600 Then .PrintQuality = 600
****************************************************************************
If .CenterHorizontally <> False Then .CenterHorizontally = False
If .CenterVertically <> False Then .CenterVertically = False
If .Orientation <> xlLandscape Then .Orientation = xlLandscape
If .Draft <> False Then .Draft = False
If .PaperSize <> xlPaperLetter Then .PaperSize = xlPaperLetter
If .FirstPageNumber <> xlAutomatic Then .FirstPageNumber = xlAutomatic
If .Order <> xlDownThenOver Then .Order = xlDownThenOver
If .BlackAndWhite <> False Then .BlackAndWhite = False
If .Zoom <> False Then .Zoom = False
'set number of pages wide to 1 and length to as required
If .FitToPagesWide <> 1 Then .FitToPagesWide = 1
If .FitToPagesTall <> False Then .FitToPagesTall = False
If .PrintErrors <> xlPrintErrorsDisplayed Then .PrintErrors = xlPrintErrorsDisplayed
If .OddAndEvenPagesHeaderFooter <> False Then .OddAndEvenPagesHeaderFooter = False
If .DifferentFirstPageHeaderFooter <> False Then .DifferentFirstPageHeaderFooter = False
If .ScaleWithDocHeaderFooter <> True Then .ScaleWithDocHeaderFooter = True
If .AlignMarginsHeaderFooter <> False Then .AlignMarginsHeaderFooter = False
If .EvenPage.LeftHeader.Text <> "" Then .EvenPage.LeftHeader.Text = ""
If .EvenPage.CenterHeader.Text <> "" Then .EvenPage.CenterHeader.Text = ""
If .EvenPage.RightHeader.Text <> "" Then .EvenPage.RightHeader.Text = ""
If .EvenPage.LeftFooter.Text <> "" Then .EvenPage.LeftFooter.Text = ""
If .EvenPage.CenterFooter.Text <> "" Then .EvenPage.CenterFooter.Text = ""
If .EvenPage.RightFooter.Text <> "" Then .EvenPage.RightFooter.Text = ""
If .FirstPage.LeftHeader.Text <> "" Then .FirstPage.LeftHeader.Text = ""
If .FirstPage.CenterHeader.Text <> "" Then .FirstPage.CenterHeader.Text = ""
If .FirstPage.RightHeader.Text <> "" Then .FirstPage.RightHeader.Text = ""
If .FirstPage.LeftFooter.Text <> "" Then .FirstPage.LeftFooter.Text = ""
If .FirstPage.CenterFooter.Text <> "" Then .FirstPage.CenterFooter.Text = ""
If .FirstPage.RightFooter.Text <> "" Then .FirstPage.RightFooter.Text = ""
End With
Case "FOO"
With wks.PageSetup
'Set all the stuff above to some other values
End With
Case Else
With wks.PageSetup
'Set all the stuff above to some other values
End With
End Select
End Sub