Lm th? no ? t?o bi?u ? Excel w/OLE t? ?ng ha t? Visual Basic

D?ch tiu ? D?ch tiu ?
ID c?a bi: 142387 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t ny minh ho? cch b?n c th? s? d?ng t? ?ng ha trong m?t Visual Basic chng tr?nh ? t?o ra m?t b?ng tnh Excel c ch?a m?t bi?u ? nhng trn m?t b?ng tnh.

THNG TIN THM

Cc b?c ? t?o ra chng tr?nh v d?

  1. B?t ?u m?t d? n m?i trong Visual Basic. Form1 ?c t?o ra b?i m?c ?nh.
  2. Thm m?t nt l?nh (Command1) vo Form1.
  3. Thm m? sau s? ki?n ny Command1_Click th? t?c:
      Private Sub Command1_Click()
       
        Dim oXL As Object        ' Excel application
        Dim oBook As Object      ' Excel workbook
        Dim oSheet As Object     ' Excel Worksheet
        Dim oChart As Object     ' Excel Chart
        
        Dim iRow As Integer      ' Index variable for the current Row
        Dim iCol As Integer      ' Index variable for the current Row
        
        Const cNumCols = 10      ' Number of points in each Series
        Const cNumRows = 2       ' Number of Series
    
        
        ReDim aTemp(1 To cNumRows, 1 To cNumCols)
        
        'Start Excel and create a new workbook
        Set oXL = CreateObject("Excel.application")
        Set oBook = oXL.Workbooks.Add
        Set oSheet = oBook.Worksheets.Item(1)
        
        ' Insert Random data into Cells for the two Series:
        Randomize Now()
        For iRow = 1 To cNumRows
           For iCol = 1 To cNumCols
              aTemp(iRow, iCol) = Int(Rnd * 50) + 1
           Next iCol
        Next iRow
        oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp
        
        'Add a chart object to the first worksheet
        Set oChart = oSheet.ChartObjects.Add(50, 40, 300, 200).Chart
        oChart.SetSourceData Source:=oSheet.Range("A1").Resize(cNumRows, cNumCols)
    
        ' Make Excel Visible:
        oXL.Visible = True
    
        oXL.UserControl = True
            
    End Sub
    					
  4. B?m phm F5 ? ch?y chng tr?nh, v b?m vo l?nh nt.
K?t qu?: Microsoft Excel b?t ?u, v m?t b?ng tnh m?i ?c t?o ra. D? li?u ng?u nhin ?c thm vo cc t? bo A1:J2 v m?t bi?u ? ?c nhng vo ngy ?u tin b?ng tnh. Nhng bi?u ? s? d?ng d? li?u ng?u nhin cho ngu?n c?a n.

Thu?c tnh

ID c?a bi: 142387 - L?n xem xt sau cng: 18 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
T? kha:
kbhowto kbprogramming kbmt KB142387 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:142387

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com