如何在VBA中组合范围?

时间:2017-01-19 14:54:02

标签: forms vba excel-vba printing excel

我有一个我在Excel中开发的表单,并添加了一个命令按钮来打印表单。表单有四个页面,但如果单元格的第二页或第三页的第一个单元格为空白,我想跳过这些页面。我已经提出了以下代码,但我得到了一个" Type Mismatch"错误,因为它结合了范围。这不是合并范围的合适方式/有更好的方法吗?

Private Sub PrintAuth1_Click()
'   Prints the Authorization Form upon clicking once

Dim printrng As Range   'Range to be printed so that blank pages are not printed.
    Set printrng = Worksheets("BLR 13210").Range("A1:AX69")
Dim ws As Worksheet
    Set ws = ActiveSheet

If ws.Range("E75") <> "" Then   'If first line of this page is blank, then it won't print the page
    printrng = printrng & ws.Range("A70:AX135")
End If
If ws.Range("E141") <> "" Then  'If first line of this page is blank, then it won't print the page
    printrng = printrng & ws.Range("A136:AX200")
End If

printrng = printrng & ws.Range("A201:AX264")    'Adds last page to print range

' Dialog Box to decide whether to quick print or make changes to printer setup.
msg = "Would you like to send to default printer?"
msg = msg & vbNewLine
config = vbYesNoCancel + vbQuestion + vbDefaultButton1
Title = "Printer Selection"
ans = MsgBox(msg, config, Title)

If ans = vbYes Then printrng.PrintOut Copies:=1, Collate:=True
If ans = vbNo Then Application.Dialogs(xlDialogPrint).Show
If ans = vbCancel Then
End If

End Sub 

0 个答案:

没有答案