2017-01-26 6 views
0

ワークシートのデータに基づいてグラフを作成しようとしています。グラフを作成するコードがあります。ワークシートをループしてVBAでグラフを作成する

問題私はグラフがすでに存在する場合、私はそれは、以前のデータをきれいにし、新しいデータを入れて活性化する場合には、チェックするために私のワークブックのワークシートをループにしようとしています 。

の場合

ワークシートの名前が一致するかどうかを確認するためにダブルループを作成しようとしましたが、動作しません(グラフを何も設定できません)。

何をすればいいですか?

現在のコード(関連部分のみ)

Set RetRange = w.Sheets("Ret").UsedRange 
    ' Set RetRange = w.Sheets("Returns Output").Range(w.Sheets("Ret").Cells("A1").SpecialCells(xlLastCell)) 

'if graph is already there, change  
Set RetChart = Nothing 

For Each ws In w.Worksheets 
    If ws.Name = "RetGraph" Then   
     Set RetChart = Charts("Ret").Activate  
    Else 

    End If 
Next ws 

If RetChart = Nothing Then 
    Set RetChart = Charts.Add   
End If 

With RetChart 
    .Select     
    .ChartType = xlLine     
    .HasTitle = True 
    .ChartTitle.Text = "Index Performance" 
    .SetSourceData Source:=RetRange 
    .Axes(xlCategory, xlPrimary).HasTitle = True 
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date" 
    .Axes(xlValue, xlPrimary).HasTitle = True 
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Return" 
    .Name = "RetGraph" 
    .Legend.Select 
    Selection.position = xlBottom 
End With 

End Sub 

答えて

0

Name "RETが" ChartObjectの財産ではなく、Chartです。

最上位の階層を使用すると、以下の短いの図を参照してください、ChartNameおよび他の多くを見つけることができ、その下、ChartObject次のとおりです。

ChartObject 
--> Chart 
    |--> ChartArea 
    |--> Axes 
    |--> SeriesCollection 
    |--> Format.Line 
--> Name 
--> Top 
--> Left 
--> Height 
--> Width 

コメント:私はChartObjectと下にあなたがすることができます簡単に巣を使用することをお勧めします他のすべてのプロパティを変更します。また、ほとんどの場合、SelectSelectionを使用する理由はありませんが、ise完全修飾ChartObjectsとそれはネストされたプロパティです。

以下のコードは、すべてWorksheetsをループし、次にWorksheetはすべてChartObjectsをループし、「Ret」を検索します。

コード

Option Explicit 

Sub LoopSheets_ChartObjects() 

Dim RetChart As Chart 
Dim ChtObj As ChartObject 
Dim ws As Worksheet 
Dim w As Workbook 

Set w = ThisWorkbook 

For Each ws In w.Worksheets 
    If ws.Name = "RetGraph" Then 
     For Each ChtObj In ws.ChartObjects 
      If ChtObj.Name = "Ret" Then '<-- if there is a match 
       Set RetChart = ChtObj.Chart ' <-- set chart to ChartObject.Chart 
      End If 
     Next ChtObj 
    End If 
Next ws 

If RetChart Is Nothing Then 
    MsgBox "NOT Found" 
    ' do your coding stuff 
End If 

End Sub 

編集1:PO新しい情報をサポートするために、チャートはグラフシートに配置されます。

Dim RetChart As Chart 
Dim Cht_Sht As Chart 

For Each Cht_Sht In w.Charts 
    If Cht_Sht.Name = "RetGraph" Then 
     Set RetChart = Cht_Sht 
    End If 
Next Cht_Sht 

If RetChart Is Nothing Then 
    MsgBox "NOT Found" 
    ' do your coding stuff 

Else ' <-- chart found 
    RetChart.ChartTitle.Text = "Test" '<-- for debug only 
End If 
+0

感謝のループを回避することができます。私はあなたがワークシートのチャートをループしていることを理解しています。私の間違いは、チャートシートにチャートを作成していることです。 私は今それをループし、チャートがすでに存在するかどうかを確認しようとしています。そうでない場合は、グラフを作成します。 – DGMS89

+0

@ DGMS89 **編集1 **の下で私の編集したコードを試してください –

0

あなたはシートを通じて答えを

Dim RetChart As Chart 

On Error Resume Next 
Set RetChart = w.Charts("RetGraph") 
On Error GoTo 0 

If RetChart Is Nothing Then '<--| chart not found 
    MsgBox "NOT Found" 
    ' do your coding stuff 
Else ' <--| chart found 
    RetChart.ChartTitle.Text = "Test" '<-- for debug only 
End If 
関連する問題