2017-01-26 7 views
0

워크 시트의 일부 데이터를 기반으로 그래프를 만들려고합니다. 그래프를 생성하는 코드가 있습니다.워크 시트를 반복하여 VBA에서 그래프를 만듭니다.

문제 I 그래프가 이미있는 경우 나는 그것이 이전 데이터를 정리하고 새로운 데이터를 넣어 작동이 경우, 확인 내 통합 문서의 워크 시트를 통해 루프를 시도하고 .

하는 경우 그렇지 않다면 새로운 그래프를 만들고 데이터를 넣습니다.

워크 시트의 이름이 일치하는지 확인하기 위해 이중 루프를 만들려고했으나 작동하지 않습니다 (차트를 아무 것도 설정할 수 없음).

어떻게해야할까요?

현재 코드 (단지 관련 부분)

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 "징벌은"ChartObject의 재산이 아닌 Chart입니다.

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

코멘트 : 나는 ChartObject를 사용하는 것이 좋습니다 한 다음 아래에 둥지를 쉽게 할 수있는 당신이 Chart, Name 및 많은 다른 사람을 찾을 수 아래의 짧은 그림을 볼 수 있습니다 그 아래 가장 높은 계층은 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 
관련 문제