2016-08-12 77 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