2016-11-21 70 views
0

我有以下VBA代碼,用於將一系列單元格導出爲jpeg到指定文件夾。我想讓它遍歷一個工作簿中的所有工作表。在將範圍導出爲圖像時循環顯示工作表

我需要幫助通過所有打開的工作簿循環此代碼。我相信我需要: Dim WS作爲工作表,然後設置一個If語句,插入下面的代碼,結束if語句,然後在最後放置一個Next WS來實際循環。我的問題是,當我試圖結合我的if語句時,我不斷收到91錯誤,對於每個WS在ThisWorkbook.Sheets中如果不是WS.Name =「Sheet2」然後,用我的代碼在下面。

以下代碼一次在一個工作表中工作。

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