修改当前打印区域以删除第一列

时间:2015-08-06 10:45:57

标签: excel vba excel-vba excel-2010

在Excel工作簿中,我有几个WorkSheets,每个工作表包含一些敏感数据和一个表(ListObject)。打印时,只有表格应在打印区域中。

要实现此目的(并设置其他打印默认值),我会使用Workbook_BeforePrint事件。我有两个subsWS的值设置为当前工作表,将Table设置为第一个ListObject。

但是,我有一张表,我无法打印第一列。我能够定位工作表/表格,但我不知道如何从打印区域中删除第一列?

.PrintArea = Table.Range.Address
If WS.Name = "FileNameLength" Then
    { remove the frist column from the print area }
End If

2 个答案:

答案 0 :(得分:1)

如果没有进一步的代码,我无法定制它,但你应该能够适应以下......

Sub StripFirstCol()

    Dim tbl As ListObject: Set tbl = ActiveSheet.ListObjects("Table1")
    Dim PrintRng As Range: Set PrintRng = tbl.Range

    Dim ws As Worksheet: Set ws = ActiveSheet

    With ws.PageSetup
        If ws.Name = "FileNameLength" Then
            .PrintArea = Intersect(PrintRng, PrintRng.Offset(0, 1)).Address
        Else
            .PrintArea = PrintRng.Address
        End If
    End With

End Sub

答案 1 :(得分:0)

您需要从打印范围中删除第一列中的单元格。

这样的事情应该让你开始:

    Dim aCell As Range
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range

    Set rng1 = Table.Range
    Set rng2 = Range("Table1[[#Headers],[columnheadertext]]")

    Set rng3 = Nothing

    For Each aCell In rng1

        If Intersect(aCell, rng2) Is Nothing Then
            If rng3 Is Nothing Then
                Set rng3 = aCell
            Else
            Set     rng3 = Union(rng3, aCell)
            End If
        End If

    Next aCell

'rng3 is your new range to be printed

 .PrintArea = rng3