How to Use Excel VBA for Printing

 1
  • Open the Excel file containing the procedure that you want to add the PrintOut Method to. Decide which of the three PrintOut Methods is best to use in your procedure.

  • 2

    Open the VBA Editor to view your procedure. A shortcut to do this is to press "Alt" and "F11." In your procedure locate the area where you want to enter the PrintOut Method. Usually this method is located at or near the end of the procedure.

  • 3

    Enter the Workbooks' PrintOut Method command by typing the following:

    Workbooks("Book1").PrintOut

    This method prints all the worksheets in the Excel workbook. Replace "Book1" in quotations with the title of your workbook.

  • 4

    Enter the Worksheets' PrintOut Method syntax as in the example. This method prints only one worksheet in the Excel workbook. However, you can enter it as many times as you have worksheets to print.

    Worksheets("Sheet1").PrintOut

    Replace Sheet1 in quotations with the worksheet tab you want to print.

  • 5

    Enter the Worksheets' Range PrintOut Method syntax as in the example. This method prints a range, a selection of cells in a worksheet. You can enter it as many times as you have ranges to print.

    Worksheets("Sheet1").Range("A1:A20").PrintOut

    Replace Sheet1 in quotations with the worksheet tab, and replace A1:A20 with the range of cells you want to print.


     

    用VBA实现在单击“打印”时,先弹出“消息框”后,点击“确认”后才开始打印?

    If MsgBox("是否要打印?", vbYesNo) <> vbYes Then Exit Sub

     

    使用循环

    Do While Sheet4.Cells(1, 18) <> .Cells(m, 1)
    Loop

     

    设置打印区域

    Sub test()
    ActiveWindow.View = xlPageBreakPreview
    For b = [h65536].End(xlUp).Row To 2 Step -1
        If Mid(Cells(b, 8), 1, 1) = "交" Then
            If x = Empty Then
                x = 1
                y = 65536
            Else
                y = x
            End If
            
            For a = Range("a" & y).End(xlUp).Row To 2 Step -1
                If Cells(a, 1) = "參考號:" Then
                    k = Range(Cells(a, 1), Cells(b, 8)).Address & "," & k 
                    x = a
                    GoTo uk
                End If
            Next
        End If
    uk: Next
    gu = 1
    ActiveSheet.PageSetup.PrintArea = k
    End Sub
    看看k的值~
    print k

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值