投稿日 | : 2004/02/05(Thu) 13:50 |
投稿者 | : ak |
Eメール | : |
URL | : |
タイトル | : Re: VBからExcelのグラフをPictureBoxに表示 |
下記サンプルを試してみてください
Private Sub Command1_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Picture1.Picture = LoadPicture()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
For a = 1 To 36
If a < 13 Then
xlSheet.Cells(1, a).Value = lblUri(a - 1).Caption
ElseIf a < 25 Then
xlSheet.Cells(2, a - 12).Value = lblUri(a - 1).Caption
Else
xlSheet.Cells(3, a - 24).Value = lblUri(a - 1).Caption
End If
Next a
With xlBook.Charts.Add
.Name = "グラフ1"
.ChartType = xlLine
.SetSourceData Source:=xlBook.Sheets(xlSheet.Name).Range("A1")
.SeriesCollection(1).Values = "=" & xlSheet.Name & "!R1C1:R1C12"
.SeriesCollection(1).Name = "=""2002年度"""
.SeriesCollection.NewSeries
.SeriesCollection(2).Values = "=" & xlSheet.Name & "!R2C1:R2C12"
.SeriesCollection(2).Name = "=""2003年度"""
.SeriesCollection.NewSeries
.SeriesCollection(3).Values = "=" & xlSheet.Name & "!R3C1:R3C12"
.SeriesCollection(3).Name = "=""2004年度"""
With .Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With .Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
.HasDataTable = False
Clipboard.Clear
.CopyPicture
If Clipboard.GetFormat(vbCFMetafile) Then
Set Picture1.Picture = Clipboard.GetData()
End If
End With
xlApp.DisplayAlerts = False
xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
追記
>On Error Resume Next
エラーを流すのはやめましょう。
Rsさんのサンプルでエラーが発生していました。
これをコメントアウトすればエラー箇所が直ぐ分かるはずです。
よほどのことが無い限り「On Error Resume Next」使用すべきでないと
思います。バグのもとです。