Excel VBA按订单数打印指定页数触发运行时1004错误求助
VBA打印触发1004错误的解决方法
问题背景
要实现按订单数量打印对应页数的功能,已将统计的订单数存入printPages变量,但执行ws.PrintOut时触发1004错误。操作流程:
- 在Requirementsheets工作表批量生成透视表,循环填充订单数据,无订单时填充"(blank)",过滤不必要物料;
- 在Orderoverzicht工作表用
WorksheetFunction.CountIf(ws.Range("A5:A1000"), ">1")统计订单数量,传入PrintOut的To参数; - 执行
ws.PrintOut From:=2, To:=printPages, Copies:=1, Collate:=True, IgnorePrintAreas:=False时出错,指定完整工作表路径也无效。
错误原因分析
- printPages参数无效:统计的订单数可能为0,或者大于工作表实际可打印的总页数,导致PrintOut的To参数超出合法范围,触发1004错误。
- 透视表未完全刷新:
ThisWorkbook.RefreshAll执行后直接打印,可能透视表还未刷新完成,导致打印逻辑异常。 - 打印区域冲突:
IgnorePrintAreas:=False时,工作表的打印区域设置可能和指定的From/To页数范围冲突。
解决步骤
- 增加
printPages的有效性校验:确保其值≥From参数(这里是2),且不超过工作表实际总打印页数;若不符合则提示或跳过打印。 - 添加透视表刷新等待逻辑,确保所有透视表更新完成后再执行打印。
- 设置
IgnorePrintAreas:=True,避免打印区域设置干扰指定的页数范围。 - 移除不必要的
Activate和Select操作,减少潜在错误触发点。
修改后的完整代码
Sub UpdatePivotTables() Dim ws As Worksheet Dim pt As PivotTable Dim rngOrders As Range Dim cell As Range Dim tableIndex As Integer Dim orderField As PivotField Dim orderItem As PivotItem Dim orderValue As String Dim printPages As Integer Dim totalPrintPages As Integer ' 设置订单所在工作表和范围 Set ws = Worksheets("Orderoverzicht") Set rngOrders = ws.Range("A5:A35") Application.ScreenUpdating = False ' 循环更新每个透视表的"Order"字段 For tableIndex = 2 To 30 ' 查找对应名称的透视表 Set pt = Nothing On Error Resume Next Set pt = Worksheets("Requirementsheets").PivotTables("Order" & tableIndex) On Error GoTo 0 ' 确认找到目标透视表 If Not pt Is Nothing And pt.Name = "Order" & tableIndex Then Debug.Print "Updating PivotTable: " & pt.Name & ", Order Value: " & orderValue ' 确认存在对应订单单元格 If tableIndex <= rngOrders.Cells.Count Then Set cell = rngOrders.Cells(tableIndex) orderValue = cell.Value ' 处理订单字段过滤 Set orderField = pt.PivotFields("Order") On Error Resume Next orderField.ClearAllFilters On Error GoTo 0 If orderValue = "" Then orderValue = "(blank)" orderField.CurrentPage = orderValue End If On Error Resume Next orderField.CurrentPage = orderValue On Error GoTo 0 ' 刷新透视表 pt.RefreshTable End If ' 过滤不需要的物料组 With pt.PivotFields("Material Group Name") .PivotItems("Bulk Lub. Interm. Cd").Visible = False .PivotItems("Empty Pack.Material").Visible = False .PivotItems("One Way IBC").Visible = False .PivotItems("Pallets").Visible = False .PivotItems("#N/A").Visible = False .PivotItems("(blank)").Visible = False .PivotItems("Base Oil Group II").Visible = False .PivotItems("Pump Off IBC").Visible = False .PivotItems("Special Products").Visible = False .PivotItems("Base Oil Group I").Visible = False .PivotItems("Base Oil Group III").Visible = False .PivotItems("Additives Bulk").Visible = False .PivotItems("Packed Lubes").Visible = False End With ' 过滤指定物料 With pt.PivotFields("Material") .PivotItems("88022000").Visible = False End With End If Next tableIndex ' 统计订单数量 Set ws = Worksheets("Orderoverzicht") printPages = WorksheetFunction.CountIf(ws.Range("A5:A1000"), ">1") ' 刷新所有数据并等待完成 ThisWorkbook.RefreshAll DoEvents Application.ScreenUpdating = True ' 获取目标工作表的实际总打印页数 Set ws = Worksheets("Requirementsheets") totalPrintPages = ws.PageSetup.Pages.Count ' 校验打印参数并执行打印 If printPages >= 2 And printPages <= totalPrintPages Then ws.PrintOut From:=2, To:=printPages, Copies:=1, Collate:=True, IgnorePrintAreas:=True ElseIf printPages < 2 Then MsgBox "订单数量不足,无需打印指定范围", vbInformation Else MsgBox "打印页数超出工作表实际页数,实际总页数为:" & totalPrintPages, vbExclamation End If End Sub
内容的提问来源于stack exchange,提问作者Jef Theeuwes
相关产品推荐
相关产品推荐

