2017-06-19 400 views
1

我在Excel中有一个工作表,其中包含多个页面。我想打印选定的页面,例如第1页,第3页,第4页和第6页。我可以在singel打印工作中做到这一点吗?使用VBA宏打印Excel工作表中的选定页面

在知道我可以用下面的命令打印的页数: ActiveSheet.PrintOut From:=1, To:=1, Copies:=1, Collate:=True ActiveSheet.PrintOut From:=1, To:=3, Copies:=3, Collate:=True ActiveSheet.PrintOut From:=1, To:=4, Copies:=4, Collate:=True ActiveSheet.PrintOut From:=1, To:=6, Copies:=6, Collate:=True

不过这样一来,我创造了很多的打印作业。因此,我期待像类似 打印输出(第1页,第3页,第4页,....)


更多信息关于我的问题: 有我的工作表一个送货单:Excel delivery notes 他们中的一些没有C栏中的条目。 (例如第2页)。我写了万客隆它迭代的pagebreakes,检查每一页上的C-柱:

Option Explicit 
Sub LieferscheineDrucken() 
Dim HPB As HPageBreak 
Dim breakAdress As Integer 
Dim pageNumber As Integer 
Dim productNumber As Integer 
Dim printCondition As Boolean 
pageNumber = 1 
printCondition = False 

'Drucke Seite 1 
For productNumber = 10 To 61 
    ActiveSheet.Cells(productNumber, "C").Select 
    If ActiveSheet.Cells(productNumber, "C").Value <> "" Then 
    printCondition = True 
    End If 
Next 

If printCondition = True Then 
    ActiveSheet.PrintOut From:=1, To:=1, Copies:=1, Collate:=True 
End If 
printCondition = False 
pageNumber = pageNumber + 1 


'Drucken der restlichen Seiten 
For Each HPB In ActiveSheet.HPageBreaks 
    breakAdress = CInt(Mid(HPB.Location.Address, 4, 5)) 


    'Teste, ob Lieferschein Einträge enthält 
    For productNumber = (breakAdress + 9) To (breakAdress + 60) 
      If ActiveSheet.Cells(productNumber, "C").Value <> "" Then 
       printCondition = True 
      End If 
    Next 

    If printCondition = True Then 
     ActiveSheet.PrintOut From:=pageNumber, To:=pageNumber, Copies:=1, Collate:=True 
    End If 

    pageNumber = pageNumber + 1 
    printCondition = False 

Next 
End Sub 

,不过该宏发PrintJob的每一页。如何通过只发送一个printjob来打印相关页面?

感谢您的帮助!

+0

据我所知,您可以对表单执行此操作,但不能处理页面,因为打印输出中没有处理页面数组的属性,而不是序列。一个技巧是将页面移动到工作表,然后尝试打印选中的工作表,并将其保存在数组中:'ThisWorkbook.Worksheets(PrintCollection).PrintOut'其中PrintCollection是一组工作表 – Ibo

+0

是的,我发现了几种方法用于打印收集表格。 复制解决方案似乎非常烦人,但我会试试这个。 谢谢。 – Hannes

回答

0

代码会是这样的。

Sub test() 
    Dim Ws As Worksheet 
    Dim hp As HPageBreak 
    Dim vAddress() 
    Dim i As Long 

    Set Ws = ActiveSheet 
    n = Ws.HPageBreaks.Count 

    For i = 0 To n 
     ReDim Preserve vAddress(i) 
     If i = 0 Then 
     vAddress(i) = Ws.Range("a1").Address 
     Else 
     vAddress(i) = Ws.HPageBreaks(i).Location.Address 
     End If 
      Set rngDB = Ws.Range(vAddress(i)).Range("c5").Resize(21) 'Range("c5").Resize (21) is coumn C date range 
      If WorksheetFunction.CountA(rngDB) Then 
       Ws.PrintOut from:=i + 1, To:=i + 1, preview:=True 
      End If 
    Next i 

End Sub