无法获得简单的打印vba脚本运行

Sub Print3() Dim sht As Worksheet Dim CurrentSheet As Worksheet Dim cht As ChartObject Application.ScreenUpdating = False Application.EnableEvents = False Set CurrentSheet = ActiveSheet For Each sht In ActiveWorkbook.Worksheets If .Name <> "CHART OVERALL" Or "CHART TL" Or "CHART BL" Or "CHART TR" Or "CHART BR" Then Else For Each cht In sht.ChartObjects cht.Activate ActiveChart.PrintOut Copies:=1 Next cht End If Next sht CurrentSheet.Activate Application.EnableEvents = True End Sub 

我似乎无法得到这个工作。 我已经尝试了很多这种代码的变体,我不知道为什么它不工作,因为它很简单。 对于我在If语句中命名的每个工作表,我想要打印这些表中的图表。 有什么想法吗?

你的if语句需要一些工作,增加了屏幕更新= true …

 Sub Print3() Dim sht As Worksheet Dim CurrentSheet As Worksheet Dim cht As ChartObject Application.ScreenUpdating = False Application.EnableEvents = False Set CurrentSheet = ActiveSheet For Each sht In ActiveWorkbook.Worksheets If .Name <> "CHART OVERALL" and .Name <> "CHART TL" and .Name <> "CHART BL" and .Name <> "CHART TR" and .Name <> "CHART BR" Then Else For Each cht In sht.ChartObjects cht.Activate ActiveChart.PrintOut Copies:=1 Next cht End If Next sht CurrentSheet.Activate Application.EnableEvents = True application.screenupdating = true End Sub 

所以我修改了一些东西。 对于if语句,我认为它一直注册为真,可能是由于.Name没有得到表名,所以你从来没有进入你的else语句。 这是修改的代码。

 Sub Print3() Dim sht As Worksheet Dim CurrentSheet As Worksheet Dim cht As ChartObject Application.ScreenUpdating = False Application.EnableEvents = False Set CurrentSheet = ActiveSheet For Each sht In ActiveWorkbook.Worksheets If sht.Name = "CHART OVERALL" Or sht.Name = "CHART TL" Or sht.Name = "CHART BL" Or sht.Name = "CHART TR" Or sht.Name = "CHART BR" Then For Each cht In sht.ChartObjects Debug.Print sht.Name cht.Activate ActiveChart.PrintOut Copies:=1 Next cht End If Next sht CurrentSheet.Activate Application.EnableEvents = True End Sub