【问题标题】:VBA Macro Error when trying to Print Sheets尝试打印工作表时出现 VBA 宏错误
【发布时间】:2020-11-20 07:47:13
【问题描述】:

我有一个宏,我想循环浏览 Excel 中的工作表并根据单元格值 B5 和 B6 打印每个工作表,问题是如果 B5 或 B6 的值为 0,则宏崩溃并显示错误,如果 B5 的值为 0,有没有办法让它跳到代码的下一部分?

非常感谢任何帮助!

Sub printlabels()

Worksheets("1033").Activate
iNumCopies = Range("B5").Value
ActiveSheet.PrintOut Copies:=iNumCopies

Worksheets("1090").Activate
iNumCopies = Range("B6").Value
ActiveSheet.PrintOut Copies:=iNumCopies

End Sub

【问题讨论】:

    标签: excel vba printing


    【解决方案1】:

    你可以使用If语句

    if iNumCopies > 0 Then
         ActiveSheet.PrintOut Copies:=iNumCopies
    End If
    

    另外,如果为每张工作表编写不同的代码,会花费很多时间。建议改为循环浏览工作表。

    For Each xSheet in ThisWorkbook.Sheets
       iNumCopies = xSheet.Range("B5").Value        ' Or iNumCopies = xSheet.Range("B6").Value
    
       if iNumCopies > 0 Then
             xSheet.PrintOut Copies:=iNumCopies
       End If
    next xSheet
    

    您不必激活工作表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      相关资源
      最近更新 更多