简体   繁体   English

合并多个工作表中的打印范围VBA EXCEL

[英]Combine Print Ranges from Multiple Worksheets VBA EXCEL

I am trying to figure out how to print the "ActiveSheet" or Sheet1 along with "Sheet5" (rows 1-6, A:M) being displayed at the bottom with a 2 row space in between the end of Sheet1 and the beginning of data from Sheet5. 我试图弄清楚如何打印“ ActiveSheet”或Sheet1以及“ Sheet5”(第1-6行,A:M)显示在底部,在Sheet1的末尾和开头之间有2行空间Sheet5中的数据。 I've been trying to look up similar questions and read something about a "Union" but I wasn't sure how it would fit here. 我一直在尝试查找类似的问题,并阅读有关“联盟”的内容,但是我不确定它在这里是否适合。

Private Sub CommandButton1_Click()
Dim Sel_Manager As String
'Headers repeated at the top
Application.PrintCommunication = False
With ActiveSheet.PageSetup
        .PrintTitleRows = "$2:$2"
        .PrintTitleColumns = "$B:$M"
        .Orientation = xlLandscape
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
End With

'Manager selection through ComboBox dropdown
 Sel_Manager = ComboBox1
'Inserting autofilters for worksheet
Cells.Select
Selection.AutoFilter
'Manager defined in the dropdown ComboBox
ActiveSheet.Range("B2", Range("M2").End(xlDown)).AutoFilter Field:=1, Criteria1:=Sel_Manager
ActiveSheet.Range("B2", Range("M2").End(xlDown)).AutoFilter Field:=2, Criteria1:="A"
 'Here I select range to be printed and specify manager in filename
ActiveSheet.Range("B2", Range("M2").End(xlDown)).Select

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Sel_Manager + ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

ActiveSheet.ShowAllData
Application.PrintCommunication = True
End Sub

This may give you some ideas. 这可能会给您一些想法。

I created two worksheets. 我创建了两个工作表。 One, called "Main", contains data that has "names" in column B and some As in column C. The other, called "Extra" contains the six rows to appear at the bottom of the filtered data. 一个称为“ Main”,包含在B列中具有“名称”的数据,而在C列中包含一些As。另一个名为“ Extra”的数据包含六行,这些行将显示在过滤数据的底部。

I do not use Excel's identifiers when referencing worksheets. 引用工作表时,我不使用Excel的标识符。 The first sheet will have an identifier of Sheet1 and a name of "Sheet1". 第一工作表将具有工作表Sheet1的标识符和名称“工作表1”。 If you immediately create another sheet it will have an identifier of Sheet2 and a name of "Sheet2". 如果立即创建另一个工作表,它将具有工作表Sheet2的标识符和名称“工作表2”。 However, if Sheet1 is renamed before the second sheet is created, it will have an identifier of Sheet2 and a name of "Sheet1". 但是,如果在创建第二个工作表之前重命名工作表Sheet1,它将具有工作表Sheet2的标识符和名称“工作表1”。 It can all get very confusing. 一切都会变得很混乱。

I have hardcoded the selected manager as "Aaaaa" rather than make it a user entered parameter. 我已将选定的经理硬编码为“ Aaaaa”,而不是使其成为用户输入的参数。 I have prepared worksheet "Main" for printing but have not output it. 我已经为打印准备了工作表“ Main”,但没有输出它。

Option Explicit
Sub Test()

  Dim Manager As String
  Dim RngFiltered As Range
  Dim RowSht1Last As Long

  Manager = "Aaaaa"

  With Worksheets("Main")

    .AutoFilterMode = False   ' Switch off auto filtering if on

    ' Find last row containing a value
    RowSht1Last = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row

    With .Range("B2:M2")
      .AutoFilter Field:=1, Criteria1:=Manager
      .AutoFilter Field:=2, Criteria1:="A"
    End With

    Set RngFiltered = .Cells.SpecialCells(xlCellTypeVisible)

    ' Just to show the filtered range.  Note, I cannot find a documented limit
    ' on the number of sub-ranges within a range. If there is a limit, I have
    ' never managed to reach it. However Range.Address has a limit which is a
    ' little under 255.
    Debug.Print Replace(RngFiltered.Address, "$", "")

    Worksheets("Extra").Range("A1:M6").Copy Destination:=.Cells(RowSht1Last + 2, "A")

    ' ###### Output visible rows

    ' Delete rows copied from Sheet5
    .Rows(RowSht1Last + 2 & ":" & RowSht1Last + 8).Delete

  End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM