2014-04-01 81 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

相关问题