2014-04-01 106 views
-1

我有名爲「圖表1」,「圖表2」,「圖表3」和「自定義圖表」的工作表。我想製作一份「自定義圖表」並將其命名爲「自定義1」。我想無限期地這樣做,以便連續的副本被命名爲「自定義2」,「自定義3」等。我的代碼成功地製作副本,但未能按預期命名它們。問題是While-End循環。 VBA拒絕它,因爲條件不是布爾值。我怎樣才能改變這個代碼來根據我的規則命名新的副本?根據現有工作表名稱命名新工作表

Sub CustomChartCopy() 
'Copy the Custom Chart to a new worksheet to preserve it 
'Note: The original data series are preserved, but no longer change with the Custom Chart macro 
Application.ScreenUpdating = False 
Application.DisplayAlerts = False 

Dim j As Integer 
Dim ws As Worksheet 

j = 1 

Set CustomChart = Sheets("Custom Chart") 

CustomChart.ChartArea.Copy 
Sheets.Add After:=Sheets(Sheets.Count) 

With ActiveSheet 
    .Paste 
    .ChartObjects("Chart 1").Activate 
End With 

ActiveChart.Location Where:=xlLocationAsNewSheet 

'delete the blank last sheet of the workbook 
With ActiveWorkbook 
    .Worksheets(.Worksheets.Count).Delete 
End With 

'Name the new chart copy 
While Not InStr(ws.name, j) 
    ActiveChart.name = "Custom " & j 
    j = j + 1 
End While 

ActiveSheet.Move _ 
    After:=ActiveWorkbook.Sheets("Custom Chart") 

ActiveWindow.zoom = 140 

Application.DisplayAlerts = True 
Application.ScreenUpdating = True 
End Sub 

回答

0
Sub CustomChartCopy() 
'Copy the Custom Chart to a new worksheet to preserve it 
'Note: The original data series are preserved, but no longer change with the Custom Chart macro 
Application.ScreenUpdating = False 
Application.DisplayAlerts = False 

Dim j As Integer 
'Dim ws As Worksheet 

Set CustomChart = Sheets("Custom Chart") 

CustomChart.ChartArea.Copy 
Sheets.Add After:=Sheets(Sheets.Count) 

With ActiveSheet 
    .Paste 
    .ChartObjects("Chart 1").Activate 
End With 

ActiveChart.Location Where:=xlLocationAsNewSheet 

'delete the blank last sheet of the workbook 
With ActiveWorkbook 
    .Worksheets(.Worksheets.Count).Delete 
End With 

'move the custom chart copy 
ActiveSheet.Move _ 
    Before:=ActiveWorkbook.Sheets("EIRP LL Archive") 

're name the custom chart copy 
On Error GoTo Error_Handler 
j = 1 
Start: 
ActiveSheet.name = "Custom" & j 
ActiveWindow.zoom = 140 
Exit Sub 

Error_Handler: 
j = j + 1 
Resume Start 

Application.DisplayAlerts = True 
Application.ScreenUpdating = True 
End Sub 
1

最大,

更換端,而與蜿蜒

「---

+0

感謝。 VBA接受Wend,但它不接受While NotStr(ws.name),因爲它不是布爾值。我希望能找到解決這個問題的方案。 – jmaz

+0

你還沒有告訴Excel什麼工作表WS變量適用於... 設置WS = ActiveSheet或設置WS =工作表(j)或... 要小心,因爲循環可以不停止運行。 確保你有辦法強制退出。 –

+0

謝謝,吉姆。我解決了它。以下解決方案 – jmaz

相關問題