通过vba打印

时间:2016-06-16 11:57:46

标签: excel vba excel-vba

我在excel中有一个2页的表格,我想通过vba打印。这就是我想要的vba:
- 将第一页准确打印到我想要​​的范围“A1:P38”
- 用第一页背靠背打印第二页“A39:P82”(这意味着我需要在一行中给出打印命令?)。如果打印出正确的范围,前后页面行应该匹配,我已经验证了它们。

目前正在发生的事情是,当我尝试打印范围“A1:P82”时,它在第一页打印“A1:P37”,其余在第二页打印。当我的同事试图打印时,它会在第一页打印“A1:P39”,其余打印在第二页。有任何想法吗?我附上了我要打印的纸张。当我尝试打印时,它会打印第一页少一行,当我的朋友尝试打印时,它会在第一页打印一行。在这两种情况下,页面匹配都会被抛弃。它只需要打印第一页直到第P38行 我的代码附在下面。有2个复选框:选择是否直接显示预览或打印,另一个选择是否用户想要查看通知消息 enter image description here

代码:

Private Sub CommandButton1_Click()
Dim rowLast As Integer
Dim pages As Integer
Dim i As Integer
Dim bins As Integer
Dim count As Integer

rowLast = Sheets("Kanban Print").Cells(Rows.count, "A").End(xlUp).Row
bins = Sheets("RecManip").Cells(4, "B").Value
If rowLast = 1 And CheckBox1.Value = True Then
    MsgBox "Common error: Nothing to print."
End If
pages = Application.WorksheetFunction.RoundUp((rowLast - 1) * (bins / 2), 0)

For i = 1 To pages
    Sheets("RecManip").Cells(1, "B").Value = i
    If CheckBox3.Value = True Then
        Range("A1:P82").PrintOut preview:=True
    Else
        Range("A1:P82").PrintOut
    End If
Next

If rowLast > 1 And CheckBox1.Value = True Then
    MsgBox (rowLast - 1) & " cards printed for " & bins & " bin system."
End If

Sheets("RecManip").Cells(1, "B").Value = 1
Range("A1").Select

End Sub

感谢。

enter image description here

1 个答案:

答案 0 :(得分:1)

感谢Gserg:

Private Sub CommandButton1_Click()
Dim rowLast As Integer
Dim pages As Integer
Dim i As Integer
Dim bins As Integer
Dim count As Integer

rowLast = Sheets("Kanban Print").Cells(Rows.count, "A").End(xlUp).Row
bins = Sheets("RecManip").Cells(4, "B").Value
If rowLast = 1 And CheckBox1.Value = True Then
    MsgBox "Common error: Nothing to print."
End If
pages = Application.WorksheetFunction.RoundUp((rowLast - 1) * (bins / 2), 0)

For i = 1 To pages
    Sheets("RecManip").Cells(1, "B").Value = i
    If CheckBox3.Value = True Then
        Range("A1:P82").PrintOut preview:=True
    Else
        Sheets("Kanban Card template").PrintOut From:=1, To:=2
    End If
Next

If rowLast > 1 And CheckBox1.Value = True Then
    MsgBox (rowLast - 1) & " cards printed for " & bins & " bin system."
End If

Sheets("RecManip").Cells(1, "B").Value = 1
Range("A1").Select

End Sub