Hi everyone,
I was looking for an script that generates an Excel file with the data and points that I wanted.
First, I used the forum "http://forums.ge-ip.com/showthread.php?t=12988" to generate a basic Excel. I just wanted to share a way to automatically generate a Chart in that Excel. If you have any questions, please tell me.
Code:
Dim Excel As Object
Set Excel = CreateObject("excel.application")
docName = Excel.Workbooks.add().name
Excel.Windows(docName).Activate
Dim strSh As String
strSh = Excel.ActiveSheet.Name
'First of all, I add some random numbers
Excel.Cells(26,1).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "14"
Excel.Cells(26,2).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "33"
Excel.Cells(26,3).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "-47"
Excel.Cells(27,1).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "29"
Excel.Cells(27,2).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "28"
Excel.Cells(27,3).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "-15"
Excel.Cells(28,1).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "47"
Excel.Cells(28,2).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "47"
Excel.Cells(28,3).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "-68"
Excel.Cells(29,1).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "61"
Excel.Cells(29,2).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "13"
Excel.Cells(29,3).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "-42"
Excel.Cells(30,1).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "87"
Excel.Cells(30,2).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "99"
Excel.Cells(30,3).Select
Excel.Selection.Borders.ColorIndex = 1
Excel.ActiveCell.FormulaR1C1 = "-49"
'Add the Chart
Excel.Charts.Add
'Defines the Type of Chart (in VBA, you can enter "xlXYScatter", but Cimplicity doesn't understand it, so you have to put the number instead)
Excel.ActiveChart.ChartType = -4169 'xlXYScatter
'Defines the position of the Chart in the Sheet
Excel.ActiveChart.Location Where:=2, Name:=strSh 'xlLocationAsObject = 2
Excel.ActiveChart.Parent.Top = Excel.ActiveChart.Parent.Parent.Range("H1").Top
Excel.ActiveChart.Parent.Left = Excel.ActiveChart.Parent.Parent.Range("H1").Left
Excel.ActiveChart.Parent.Width = Excel.Range("H1:N1").Width
Excel.ActiveChart.Parent.Height = Excel.Range("A1:A20").Height
'Defines the Title
Excel.ActiveChart.HasTitle = True
Excel.ActiveChart.ChartTitle.Characters.Text = "Chart1"
'Defines the Data Values of the Chart
Excel.ActiveChart.SetSourceData Excel.Sheets(strSh).Range("A26:B30")
Excel.ActiveChart.SeriesCollection.NewSeries
Excel.ActiveChart.SeriesCollection(1).Name = "Serie1"
Excel.ActiveChart.SeriesCollection(1).XValues = "='" & strSh & "'!A26:A30"
Excel.ActiveChart.SeriesCollection(1).Values = "='" & strSh & "'!B26:B30"
Excel.ActiveChart.SeriesCollection(2).Name = "Serie2"
Excel.ActiveChart.SeriesCollection(2).XValues = "='" & strSh & "'!A26:A30"
Excel.ActiveChart.SeriesCollection(2).Values = "='" & strSh & "'!C26:C30"
'X axis name
Excel.ActiveChart.Axes(1, 1).HasTitle = True
Excel.ActiveChart.Axes(1, 1).AxisTitle.Characters.Text = "Dist along train (m)"
'Y-axis name
Excel.ActiveChart.Axes(2, 1).HasTitle = True
Excel.ActiveChart.Axes(2, 1).AxisTitle.Characters.Text = "kN"
Excel.Cells(1,1).Select
Excel.Visible = True
Set Excel = Nothing