宏打印命名范围

时间:2013-02-12 23:01:51

标签: excel excel-vba excel-2010 vba

我正在尝试组装一个宏来打开一个选择打印机的对话框,然后打印具有特定属性的特定命名范围。我从一个很好的小测试语句开始。

Sub test()
' test Macro

    Application.Dialogs(xlDialogPrinterSetup).Show
    ActiveSheet.PageSetup.PrintArea = "Print_20_Year"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False

End Sub

然后我搬进了更大的枪支,我无法让它正常工作。我在宏中添加了MsgBox,以查看是否有任何项目在触发。

没有错误,没有MsgBox弹出窗口。有什么想法吗?

编辑这是我新的当前代码。它一切正常,buuut一旦我取消注释的原则,它就会断裂。这是通过录制宏直接提供的代码。

Sub Print_20_Year()
'
' Print_20_Year Macro
'
'
'
'    Range("Print_20_Year").Select
    MsgBox "Step .4"
    Application.Dialogs(xlDialogPrinterSetup).Show
    MsgBox "Step .6"
    Application.PrintCommunication = False
   MsgBox "Step .7"
'    With ActiveSheet.PageSetup
'        .PrintTitleRows = "$4:$13"
    MsgBox "Step .8"
'        .PrintTitleColumns = ""
'    End With
    MsgBox "Step .9"
    Application.PrintCommunication = True
    MsgBox "Step 1"
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperTabloid
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = False
        .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
    MsgBox "Step 2"
    ActiveSheet.PageSetup.PrintArea = "Print_20_Year"
    MsgBox "Step 3"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
End Sub

2 个答案:

答案 0 :(得分:0)

如果你没有得到任何MsgBox弹出窗口,那么你甚至都没有超过前几行。我怀疑一开始就出现了问题:

Application.Dialogs(xlDialogPrinterSetup).Show
    ActiveSheet.PageSetup.PrintArea = "Print_20_Year"
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$4:$13"
        .PrintTitleColumns = ""    <<<< this worries me. What are you trying to do?
    End With
    MsgBox "Step 1"     <<<<< if you don't get here, one of the lines before this causes a silent error...

我建议在你的sub中添加On Error GoTo ErrorHandler语句作为第一行;然后添加(通常在子结束之前)

ErrorHandler:
    MsgBox "Oops - an error occurred. " & Err.Description

这可能需要您更多的调查来解决......

答案 1 :(得分:0)

经过多次探讨后,我意识到printtitlerows以某种方式与我在下面写的单独的UDF进行交互:

Function IsFormula(c)
IsFormula = c.HasFormula
End Function

此UDF与条件格式相关联,该条件格式突出显示其中包含公式的单元格。我们使用它来轻松识别我们必须手动更改/输入的信息。我无法解释为什么UDF正在创建该问题,但是当我删除它时,我的宏运行正常。再次添加我的公式宏后,我刚刚在代码的开头和结尾添加了Application.ScreenUpdating = FalseApplication.ScreenUpdating = True。这解决了这个问题。

相关问题