Friday, April 2, 2010

VBScript - Creating Line Charts in Microsoft Excel

VBScript - Creating Line Charts in Microsoft Excel


Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()

Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1,2) = "Monday"
objWorksheet.Cells(1,3) = "Tuesday"
objWorksheet.Cells(1,4) = "Wednesday"
objWorksheet.Cells(1,5) = "Thursday"
objWorksheet.Cells(1,6) = "Friday"

objWorksheet.Cells(2,1) = "atl-dc-01"
objWorksheet.Cells(2,2) = 82
objWorksheet.Cells(2,3) = 85
objWorksheet.Cells(2,4) = 91
objWorksheet.Cells(2,5) = 178
objWorksheet.Cells(2,6) = 91

objWorksheet.Cells(3,1) = "atl-dc-02"
objWorksheet.Cells(3,2) = 104
objWorksheet.Cells(3,3) = 87
objWorksheet.Cells(3,4) = 93
objWorksheet.Cells(3,5) = 100
objWorksheet.Cells(3,6) = 96

objWorksheet.Cells(4,1) = "atl-dc-03"
objWorksheet.Cells(4,2) = 78
objWorksheet.Cells(4,3) = 76
objWorksheet.Cells(4,4) = 95
objWorksheet.Cells(4,5) = 90
objWorksheet.Cells(4,6) = 91

Set objRange = objWorksheet.UsedRange
objRange.Select

Set colCharts = objExcel.Charts
colCharts.Add()

Set objChart = colCharts(1)
objChart.Activate

objChart.ChartType = 65

objChart.PlotArea.Fill.PresetGradient 1,1,7

objChart.SeriesCollection(1).Border.Weight = -4138
objChart.SeriesCollection(2).Border.Weight = -4138
objChart.SeriesCollection(3).Border.Weight = -4138

objChart.SeriesCollection(1).Border.ColorIndex = 2
objChart.SeriesCollection(1).MarkerBackgroundColorIndex = 2

objChart.SeriesCollection(2).MarkerForegroundColorIndex = 1
objChart.SeriesCollection(3).MarkerForegroundColorIndex = 1

objChart.HasTitle = True
objChart.ChartTitle.Text = "Attempted Logons"
objChart.ChartTitle.Font.Size = 18

objChart.ChartArea.Fill.Visible = True
objChart.ChartArea.Fill.PresetTextured 15

objChart.ChartArea.Border.LineStyle = 1

objChart.HasLegend = True
objChart.Legend.Shadow = True

[Via]