VBA在Excel中画折线图解决办法

VBA在Excel中画折线图
Option   Explicit
Dim   Appxls   As   New   Application
Dim   WorkBook   As   WorkBook
Dim   AppSheet   As   Worksheet
Dim   PathName   As   String

Private   Sub   Command1_Click()
Dim   i   As   Integer
Randomize
        Me.CommonDialog1.Filter   =   "*.xls|*.xls "
        Me.CommonDialog1.ShowOpen
        PathName   =   Me.CommonDialog1.FileName
        Set   WorkBook   =   Appxls.Workbooks.Open(PathName)
        Set   AppSheet   =   WorkBook.Worksheets( "sheet1 ")
        For   i   =   1   To   4
                AppSheet.Cells(i,   "A ")   =   CInt(Rnd   *   200)
        Next
        For   i   =   1   To   4
                AppSheet.Cells(i,   "B ")   =   CInt(Rnd   *   100)
        Next
        For   i   =   1   To   4
                AppSheet.Cells(i,   "C ")   =   CInt(Rnd   *   100)
        Next
        Dim   x   As   Chart             'Create   an   embedded   chart   on   a   chart   sheet.
        Set   x   =   Charts.Add
        x.ChartType   =   xlLine
        x.SetSourceData   Source:=Sheets( "Sheet1 ").Range( "A1:C4 ")
        Set   x   =   x.Location(Where:=xlLocationAsObject,   Name:= "Sheet1 ")
        '       Move     chart     to     new     sheet     and     reset     the     chart     object     variable     x.
'         Set   x   =   x.Location(Where:=xlLocationAsNewSheet,   Name:= "Chart1 ")
        '       Move     chart     to     back     to     the     chart     sheet.
End   Sub
Private   Sub   Form_Load()
      Me.Show
      Set   Appxls   =   Excel.Application

End   Sub

Private   Sub   Form_QueryUnload(Cancel   As   Integer,   UnloadMode   As   Integer)
        WorkBook.Save                                               'save   file(*.xls)
       
        AppSheet.Application.Quit                       'close   EXCEL.EXE   in   Windows
       
        Set   AppSheet   =   Nothing
        Set   WorkBook   =   Nothing