自定义打印区域宏

时间:2018-05-23 13:21:05

标签: excel excel-vba vba

我正在创建一个宏,将打印区域设置为用户选择的文档区域。基本上,一堆单元格旁边有一个框,如果用户勾选该框,则打印区域中会包含一堆单元格。 到目前为止,这是我的代码:

Sub TestCellA1()

    Dim t As Integer, d As Integer
    t = 0
    d = 20
    Dim rng_per As Range
    Set rng_per = Range("A3:E328") 'prints whole document
    Dim rng1 As Range

    If Not IsEmpty(Range("F19")) = True Then
        ActiveSheet.PageSetup.PrintArea = Range(rng_per)

    Else
        Do While t < 10
            If IsEmpty(Range("F" & d).Value) = True Then
                'MsgBox "Do not print"
            Else
                'MsgBox "Do print"
                ActiveSheet.PageSetup.PrintArea = rng1
            End If
            t = t + 1
            d = d + 25

        Loop

    End If
End Sub

到目前为止,这工作到了应该完成实际工作的程度。我计划每当循环找到框勾选时,它会将该部分文档添加到打印区域。作为vba的新手,我不知道如何将这些区域添加到打印区域。有什么想法怎么做?在此先感谢&amp;祝你有个美好的一天。

2 个答案:

答案 0 :(得分:2)

如果您创建并将范围加载到rng_to_add,则以下内容将采用现有的PrintArea和Union(附加到)rng_to_add

' Going into this, you need to have declared a variable called rng_to_add
Dim rng_to_add As Range

' and loaded the range area you want to add to the PrintArea. This will
' be different for your particular situation.
Set rng_to_add = Sheets("Sheet1").Range("A1:C3")

' Referring to the current PageSetup of the Activesheet..
With ActiveSheet.PageSetup

    ' Check if the PrintArea of above PageSetup is empty
    If .PrintArea = "" Then
        ' If so, set the PrintArea to the address of the Range: rng_to_add
        .PrintArea = rng_to_add.Address
    Else
        ' If not, set it to the address of a union (append) of the existing
        ' PrintArea's range and the address of the Range: rng_to_add
        .PrintArea = Union(Range(.PrintArea), rng_to_add).Address
    End If

' End the reference to the current PageSetup of the Activesheet
End With

因此,为了便于移植和/或集成到现有例程中,您可以创建管理PrintArea的子例程,如下所示:

Sub Clear_PrintArea()
    ' Set PrintArea to nothing
    ActiveSheet.PageSetup.PrintArea = ""
End Sub


Sub Add_range_to_PrintArea(rng_to_add As Range)

    ' Referring to the current PageSetup of the Activesheet..
    With ActiveSheet.PageSetup

        ' Check if the PrintArea of above PageSetup is empty
        If .PrintArea = "" Then
            ' If so, set the PrintArea to the address of the Range: rng_to_add
            .PrintArea = rng_to_add.Address
        Else
            ' If not, set it to the address of a union (append) of the existing
            ' PrintArea's range and the address of the Range: rng_to_add
            .PrintArea = Union(Range(.PrintArea), rng_to_add).Address
        End If

    ' End the reference to the current PageSetup of the Activesheet
    End With

End Sub

然后您可以调用,如下所示:

Clear_PrintArea
Add_range_to_PrintArea Range("A1:C3")
Add_range_to_PrintArea Range("A7:C10")
Add_range_to_PrintArea Range("A13:C16")

答案 1 :(得分:0)

你可以用我假设的几种不同方式做到这一点,但这是我的建议:

  • 您将为复选框指定一个单元格。分配一个公式,如果值为true(如果选中框),则创建示例范围&#34; A1:B6&#34; (相应地改变)。

  • 在宏代码循环中,单元格范围为空或包含范围(在我的建议中,您可以使用循环):

    Sub Test()
    
    Rng = ""
    For X = 1 To 10 'Or whatever the number of your last used row would be
        If Cells(X, 1).Value <> "" Then
            If Rng = "" Then
                Rng = Cells(X, 1).Value
            Else
                Rng = Rng & "," & Cells(X, 1).Value
            End If
        End If
    Next X
    
    If Rng = "" then Rng = "A3:E328" 'Print whole range if no checkbox is checked
    ActiveSheet.PageSetup.PrintArea = Range(Rng).Address
    
    End Sub
    

将此宏指定给所有复选框并修改它。它应该适合你(无法测试)