2017-05-24 21 views
0

新しいワークブックのチャートシートに14個の散布図を追加しようとしています。次のコードは、ダイナミック配列に新しいグラフを格納しません。vbaのChartの配列に要素を追加する方法

Sub GenerateViewSheets() 

    Dim curBook As Workbook 
    ' Create new worksheet 
    Dim NewBook As Workbook 
    Set NewBook = Workbooks.Add 

    'Add a sheet 
    Dim chrt As Chart 
    Set chrt = NewBook.Charts.Add(Before:=Worksheets(Worksheets.Count)) 
    chrt.Activate 

    'Add charts 
    Dim nCharts As Integer 
    Dim iChrt As Integer 
    Dim chartArray() As Chart 
    nCharts = 14 
    ReDim chartArray(1 To nCharts) As Chart 

    With chrt 
     For i = 1 To nCharts 
     Set chartArray(i) = .ChartObjects.Add(1, 1, 20, 20) ' Left, Top, Width, Height 
     Next i 
    End With 
    End Sub 

エラーメッセージがChartObjectにまで

Error # 13 
    Generated by VBAProject 
    Error Line: 0 
    Error Desc: Type mismatch 

答えて

1

変更アレイタイプである:

Sub GenerateViewSheets() 

    Dim curBook As Workbook 
    Dim NewBook As Workbook 

    Set NewBook = Workbooks.Add 

    'Add a sheet 
    Dim chrt As Chart 
    Set chrt = NewBook.Charts.Add(Before:=Worksheets(Worksheets.Count)) 
    chrt.Activate 

    'Add charts 
    Dim nCharts As Integer 
    Dim iChrt As Integer 
    Dim chartArray() As ChartObject 

    nCharts = 14 
    ReDim chartArray(1 To nCharts) As ChartObject 

    With chrt 
     For i = 1 To nCharts 
     Set chartArray(i) = .ChartObjects.Add(1, 1, 20, 20) ' Left, Top, Width, Height 
     Next i 
    End With 

End Sub 
関連する問題