2010年03月27日


VBScript : Excel2007 のグラフ作成

Excel_chart

Microsoft のサンプルと全く同じでは動きませんでしたので、マクロを記録して
コードを補完しました。
Cells で、行・カラムにしているのはそのほうがプログラミングで都合がいいからです

関連する記事

PowerShell2.0 : COM 経由で Excel のグラフを作成した後、PDFとしてエクスポートする


<JOB>
<SCRIPT
	language="VBScript"
	src="http://lightbox.on.coocan.jp/webwsh/GetScriptDir.vbs"
></SCRIPT>

<SCRIPT language=VBScript>
' ***********************************************************
' 新しいグラフを作成する
' ***********************************************************

Dim ExcelApp	' アプリケーション
Dim ExcelBook	' ブック
Dim MySheet
Dim dataRange
Dim chartObjects
Dim newChartObject
Dim MyChart
Dim BookPath

Set ExcelApp = CreateObject("Excel.Application")

' 警告を出さないようにする
ExcelApp.DisplayAlerts = False
' Excel を表示状態にする
'ExcelApp.Visible = True

' ブック追加
ExcelApp.Workbooks.Add

' 追加したブックを取得
Set ExcelBook = ExcelApp.Workbooks( ExcelApp.Workbooks.Count )

' Worksheet を選択 ( 最初のシート )
Set MySheet = ExcelBook.Sheets(1)

' シートへグラフ用データをセット
MySheet.Cells(1, 1) = ""
MySheet.Cells(1, 2) = "Q1"
MySheet.Cells(1, 3) = "Q2"
MySheet.Cells(1, 4) = "Q3"
MySheet.Cells(1, 5) = "Q4"

MySheet.Cells(2, 1) = "N. America"
MySheet.Cells(2, 2) = "1.5"
MySheet.Cells(2, 3) = "2"
MySheet.Cells(2, 4) = "1.5"
MySheet.Cells(2, 5) = "2.5"

MySheet.Cells(3, 1) = "S. America"
MySheet.Cells(3, 2) = "2"
MySheet.Cells(3, 3) = "1.75"
MySheet.Cells(3, 4) = "2"
MySheet.Cells(3, 5) = "2"
 
MySheet.Cells(4, 1) = "Europe"
MySheet.Cells(4, 2) = "2.25"
MySheet.Cells(4, 3) = "2"
MySheet.Cells(4, 4) = "2.5"
MySheet.Cells(4, 5) = "2"

 
MySheet.Cells(5, 1) = "Asia"
MySheet.Cells(5, 2) = "2.5"
MySheet.Cells(5, 3) = "2.5"
MySheet.Cells(5, 4) = "2"
MySheet.Cells(5, 5) = "2.75"

' データの範囲
Set dataRange = MySheet.Range(MySheet.Cells(1, 1), MySheet.Cells(5, 5))

Set chartObjects = MySheet.ChartObjects()
' 座標は、グラフのエリア
Set newChartObject = chartObjects.Add(0, 100, 300, 300)

Dim paramChartFormat : paramChartFormat = 1
Dim paramCategoryLabels : paramCategoryLabels = 0
Dim paramSeriesLabels : paramSeriesLabels = 0
Dim paramHasLegend  : paramHasLegend = True
Dim paramTitle : paramTitle = "Sales by Quarter"
Dim paramCategoryTitle : paramCategoryTitle = "Fiscal Quarter"
Dim paramValueTitle : paramValueTitle = "Billions"

Set MyChart = newChartObject.Chart

MyChart.ChartType = 54
Call MyChart.SetSourceData( dataRange )

BookPath = GetScriptDir & "\chart.xls"
on error resume next
' 保存
' 56 は、Excel 2007 で拡張子 .xls で保存する場合に必要
if CLng(Left(ExcelApp.Version & "",2 )) > 11 then
	Call ExcelBook.SaveAs( BookPath, 56 )
else
	Call ExcelBook.SaveAs( BookPath )
end if
if Err.Number <> 0 then
	MsgBox( "ERROR:" & Err.Description )
end if
on error goto 0

' Excel をアプリケーションとして終了
ExcelApp.Quit
' Excel を VBScript から開放
Set ExcelApp = Nothing
' オブジェクト変数を通常変数として初期化
ExcelApp = Empty


</SCRIPT>
</JOB>

関連する記事

VBScript : 実行中のスクリプトが存在するディレクトリ
VBScript : Excel の新しいBookを作成する


関連する Microsoft ドキュメント

Creating Basic Column Charts in Excel 2007
XlChartType Enumeration [Excel 2007 Developer Reference]


タグ:EXCEL VBScript
posted by at 2010-03-27 14:51 | サンプル | このブログの読者になる | 更新情報をチェックする
×

この広告は90日以上新しい記事の投稿がないブログに表示されております。