VB利用EXCEL画趋势图
求助VB利用EXCEL画趋势图
各位大侠,我想用以下代码实现画趋势图的功能,即安排两个按键,分别话A列和B列的图并显示,遇到了RANGE方法的global问题,网上方法很多,但是都不太好用,请大家指点
PS:为了显示做得图,我把EXCEL的关闭程序注释掉了,点Command1可以做出来,但是关闭图后,再点击Command2就会报错
,即使我使用注释掉的程序,但是还是会有此问题,而且进程里的EXCEL.EXE关不掉,请大家赐教,要是能有别的方式完成趋势图也可以说说,但是数据是保存在EXCEL里的不同列中。
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Private Sub Command1Click()
Range("A:A").Select
xlApp.DisplayAlerts = False
Charts.Add.ChartWizard gallery:=xlLine, HasLegend:=False, Title:="折线图表", ValueTitle:="PH值", _
Format:=2
xlApp.Visible = True
' Set xlSheet = Nothing
' xlBook.Close
' Set xlBoook = Nothing
' xlApp.Quit
' Set xlApp = Nothing
End Sub
Private Sub Command2Click()
Range("B:B").Select
xlApp.DisplayAlerts = False
Charts.Add.ChartWizard gallery:=xlLine, HasLegend:=False, Title:="折线图表", ValueTitle:="PH值", _
Format:=2
xlApp.Visible = True
' Set xlSheet = Nothing
' xlBook.Close
' Set xlBoook = Nothing
' xlApp.Quit
' Set xlApp = Nothing
End Sub
VisualBasic程序开发环境 程序开发 VisualBasic
Answers
'引用Microsoft Excel 9.0 Object Library (后面为版本号)
Dim objExlApp As New Excel.Application
Private Sub Command1Click()
objExlApp.Workbooks.Add
objExlApp.Charts.Add
objExlApp.ActiveChart.ChartType = xlLine
objExlApp.ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("O24"), PlotBy:= _
xlRows
objExlApp.ActiveChart.SeriesCollection.NewSeries
objExlApp.ActiveChart.SeriesCollection.NewSeries
objExlApp.ActiveChart.SeriesCollection(1).XValues = "={2003,2004,2004,2005,2006}"
objExlApp.ActiveChart.SeriesCollection(1).Values = "={100,200,300,400,500}"
objExlApp.ActiveChart.SeriesCollection(1).Name = "=""第一记录"""
objExlApp.ActiveChart.SeriesCollection(2).Values = "={900,1000,300,1000,400}"
objExlApp.ActiveChart.SeriesCollection(2).Name = "=""第二记录"""
objExlApp.ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
With objExlApp.ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
objExlApp.Visible = True
End Sub
Private Sub FormQueryUnload(Cancel As Integer, UnloadMode As Integer)
objExlApp.Quit
Set objExlApp = Nothing
End Sub
'不知道这个可以没