2016-08-12 76 views
1
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语句中命名的每个工作表,我希望打印这些表中的图表。有什么想法吗?不能得到一个简单的印刷vba脚本运行

+0

我还没有完全做到这一步,但我也想能够根据另一张纸张的值选择要打印的纸张。但首先我需要把它弄下来... – user2516746

+0

sht.name <> x或sht.name = y或sht.name = z .... –

+0

你想在certs:Application的最后添加这个小漂亮的东西。 ScreenUpdating = True如果没有这些,不会变得很远...... – User632716

回答

1

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

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 
+0

我收到“无效或不合格的参考”错误。 – user2516746

+0

希望我的回答会有所帮助,但我得到这个错误是因为我需要使用sht.Name来获取表单的名称,并且不能使用.Name –

1

所以我修改了几件事情。对于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