在EXCEL打印时怎么样设置只打印偶数页呢? 用宏!

2024-11-20 23:31:38
推荐回答(2个)
回答1:

转载,希望能帮到你

在EXCEL中新建一个宏,取名为"printpage",点击“创建”,代码如下:
Dim StrTS As String, StrPage As String, StrPage1 As String, i As Integer
Dim Pages As Long, PrintFlag As Integer, Cho As Integer
Dim Aa As Integer, Bb As String, Cc As String, myPrompt1 As String
myPrompt1 = "在打印时发生错误,请检查你的打印机设置"
Pages = ExecuteExcel4Macro("Get.Document(50)") '统计总页数
StrTS = "请输入需要打印的起始页及结束页,然后点击确定" + Chr(13) + Chr(10) + "您输入的起始页将决定奇偶页" + Chr(13) + Chr(10) + "例如:1-31"
StrPage1 = "1-" & Pages
PrintFlag = 1
On Error Resume Next
Do While PrintFlag = 1
StrPage = InputBox$(StrTS, "输入页码范围[奇偶页打印]", StrPage1)
If Trim$(StrPage) <> "" Then
'判断输入页码的合法性
Aa = InStr(StrPage, "-")
If Aa = 0 Or Aa = 1 Or Aa = Len(Trim(StrPage)) Then
MsgBox "对不起,输入的页码有误"
Exit Sub
Else
Bb = Val(Left$(StrPage, Aa - 1))
Cc = Val(Right$(StrPage, Len(Trim(StrPage)) - Aa))
If IsNumeric(Bb) And IsNumeric(Cc) Then
If Bb > Cc Then
MsgBox "对不起,您输入的起始页大于结束页,打印取消!"
Exit Sub
End If
If Cc > Pages Then Cc = Pages
If (Pages = 0) Then '如果为零,说明没有可打印内容,退出程序
MsgBox "Microsoft Excel 未发现任何可以打印的内容", 0 + 48
Exit Sub
End If
If (Pages = 1) Then '判断是否只有一页,如果是,只打印第一页,然后退出
ActiveSheet.PrintOut
If Err.Number = 1004 Then
MsgBox myPrompt1, 0 + 48 '提示用户发生打印错误
End If
Exit Sub
End If
For i = Int(Bb) To Int(Cc) Step 2 '设置循环,打印奇数页或偶数页
ActiveSheet.PrintOut From:=i, To:=i
If Err.Number = 1004 Then
MsgBox myPrompt1, 0 + 48
Exit Sub
End If
Next i
Cho = MsgBox("是否续继打印?", vbYesNo + vbQuestion + vbDefaultButton1, "提示")
If Cho = vbYes Then
PrintFlag = 1
Else
PrintFlag = 0
End If
Else
MsgBox "对不起,输入的页码有误,打印取消!"
Exit Sub
End If
End If
Else
MsgBox "您未选择任何页面范围,打印取消!"
Exit Sub
End If
Loop
将这段代码复制到“Sub printpage()”和“End Sub”之间就行了,点击“执行”
以上代码可以实现在EXCEL中指定页码范围的奇偶打印功能

回答2:

For x = 1 To ThisWorkbook.Sheets.Count
y = ThisWorkbook.Sheets(x).Name
if x Mod 2 = 0 then
ThisWorkbook.Sheets(x).PrintOut
end if
Next