2016-11-21 7 views
0

次のVBAコードを使用して、ある範囲のセルをjpegに指定したフォルダにエクスポートできます。 1つのワークブック内のすべてのワークシートをループさせたいと思います。範囲をイメージとしてエクスポートするときにループするワークシート

すべての開いているブックからこのコードをループするのに役立つ必要があります。 Dim WSをワークシートとして設定し、Ifステートメントを設定し、次のコードを挿入してifステートメントを終了し、最後に実際にループするように次のWSを配置します。私の問題は、私がif文を結合しようとすると、91エラーが発生するということです。ThisWorkbook.Sheets In EachWorkbook.Sheets If Not WS.Name = "Sheet2"次に、私のコードを以下に示します。

次のコードは、一度に1つのワークシートで機能します。

Sub ExportAsImage() 
Dim objPic As Shape 
Dim objChart As Chart 
Dim i As Integer 
Dim intCount As Integer 
'copy the range as an image 
Call ActiveSheet.Range("A1:F2").CopyPicture(xlScreen, xlPicture) 
'remove all previous shapes in the ActiveSheet 
intCount = ActiveSheet.Shapes.Count 
For i = 1 To intCount 
    ActiveSheet.Shapes.Item(1).Delete 
Next i 
'create an empty chart in the ActiveSheet 
ActiveSheet.Shapes.AddChart 
'select the shape in the ActiveSheet 
ActiveSheet.Shapes.Item(1).Select 
ActiveSheet.Shapes.Item(1).Width = Range("A1:F2").Width 
ActiveSheet.Shapes.Item(1).Height = Range("A1:F2").Height 
Set objChart = ActiveChart 
'clear the chart 
objChart.ChartArea.ClearContents 
'paste the range into the chart 
objChart.Paste 
'save the chart as a JPEG 
objChart.Export ("C:\Users\------\Desktop\Test\" & Range("B2").Value &  ".jpg") 
'remove all shapes in the ActiveSheet 
intCount = ActiveSheet.Shapes.Count 
For i = 1 To intCount 
    ActiveSheet.Shapes.Item(1).Delete 
Next i 
End Sub 
+0

コードをすべて編集するように編集してください。 – SJR

答えて

1

あなたのモジュールにこれを追加します。

Sub MAIN() 
    Dim sh As Worksheet 
    For Each sh In Sheets 
     sh.Activate 
     Call ExportAsImage 
    Next sh 
End Sub 

し、それを実行します。 (コードを変更する必要はありません)

+0

ありがとう!それは素晴らしいことです。 – ChuckMac

関連する問題