Lm th? no ? t? ?ng ho Microsoft Excel t? Visual Basic

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

? Trang ny


Bi vi?t ny ch?ng t? lm th? no ? t?o v thao tc Excel b?ng cch s? d?ng t? ?ng ha t? Visual Basic.


C hai cch ? ki?m sot m?t my ch? t? ?ng ha: b?i b?ng cch s? d?ng ho?c mu?n rng bu?c ho?c ?u rng bu?c. V?i h?u rng bu?c, phng php l khng rng bu?c cho ?n khi th?i gian ch?y v cc my ch? t? ?ng ha tuyn b? nh l ?i t?ng. V?i s?m rng bu?c, ?ng d?ng c?a b?n bi?t t?i th?i gian thi?t k? lo?i chnh xc c?a ?i t?ng n s? lin l?c v?i, v c th? tuyn b? cc ?i t?ng c?a n nh l m?t lo?i h?nh c? th?. M?u ny s? d?ng ?u rng bu?c, v?n ?c coi l t?t hn trong h?u h?t tr?ng h?p b?i v? n dnh hi?u su?t l?n hn v t?t hn lo?i an ton.

? c?n ?u rng bu?c vo m?t my ch? t? ?ng ha, b?n ph?i thi?t l?p m?t tham chi?u ?n th vi?n lo?i c?a my ch?. Trong Visual Basic, i?u ny ?c th?c hi?n thng qua cc ti li?u tham kh?o h?p tho?i t?m th?y d?i d? n | Ti li?u tham kh?o tr?nh n. Cho m?u ny, b?n s? c?n ph?i thm m?t tham chi?u ?n th vi?n ki?u cho Excel tr?c khi b?n c th? ch?y cc M?. Xem cc b?c d?i y lm th? no ? thm ti li?u tham kh?o.

Xy d?ng m?u t? ?ng ha

  1. B?t ?u Visual Basic v t?o ra m?t d? n m?i c?a tiu chu?n EXE. Form1 ?c t?o ra theo m?c ?nh.
  2. Nh?p voD an v sau nh?p vo Tham khao. Cc Tham khaoh?p tho?i s? xu?t hi?n. Cu?n xu?ng danh sch cho ?n khi b?n t?m th?y Microsoft Excel ?i t?ng th vi?n, v sau ch?n m?c ? thm m?t ti li?u tham kh?o ? Excel. N?u th vi?n chnh xc ?i t?ng c?a b?n Phin b?n c?a Excel khng khng xu?t hi?n trong danh sch, h?y ch?c ch?n r?ng b?n c phin b?n Excel c?a b?n ng cch ci ?t.

    Chu y
    • N?u b?n ang t? ?ng ho Microsoft Office Excel 2007, lo?i th vi?n s? xu?t hi?n nh Microsoft Excel 12,0 ?i t?ng th vi?n trong cc Tham khaodanh sch.
    • N?u b?n ang t? ?ng ho Microsoft Office Excel 2003, lo?i th vi?n s? xu?t hi?n nh Microsoft Excel 11,0 ?i t?ng th vi?n trong cc Tham khaodanh sch.
    • N?u b?n ang t? ?ng ho Microsoft Excel 2002, th vi?n ki?u xu?t hi?n nhMicrosoft Excel 10,0 ?i t?ng th vi?n trong cc Tham khao danh sch
    • N?u b?n ang t? ?ng ho Microsoft Excel 2000, th vi?n ki?u xu?t hi?n nhMicrosoft Excel 9,0 ?i t?ng th vi?n trong cc Tham khao danh sch.
    • N?u b?n ang t? ?ng ho Microsoft Excel 97, th vi?n ki?u xu?t hi?n nhMicrosoft Excel 8,0 ?i t?ng th vi?n trong cc Tham khao danh sch
  3. Nh?p vo Ok ? ng nh?ng Tham khao h?p tho?i.
  4. Thm m?t CommandButton ? Form1.
  5. Trong c?a s? m? cho Form1, chn sau y M? s?:
       Option Explicit
       Private Sub Command1_Click()
          Dim oXL As Excel.Application
          Dim oWB As Excel.Workbook
          Dim oSheet As Excel.Worksheet
          Dim oRng As Excel.Range
          'On Error GoTo Err_Handler
       ' Start Excel and get Application object.
          Set oXL = CreateObject("Excel.Application")
          oXL.Visible = True
       ' Get a new workbook.
          Set oWB = oXL.Workbooks.Add
          Set oSheet = oWB.ActiveSheet
       ' Add table headers going cell by cell.
          oSheet.Cells(1, 1).Value = "First Name"
          oSheet.Cells(1, 2).Value = "Last Name"
          oSheet.Cells(1, 3).Value = "Full Name"
          oSheet.Cells(1, 4).Value = "Salary"
       ' Format A1:D1 as bold, vertical alignment = center.
          With oSheet.Range("A1", "D1")
             .Font.Bold = True
             .VerticalAlignment = xlVAlignCenter
          End With
       ' Create an array to set multiple values at once.
          Dim saNames(5, 2) As String
          saNames(0, 0) = "John"
          saNames(0, 1) = "Smith"
          saNames(1, 0) = "Tom"
          saNames(1, 1) = "Brown"
          saNames(2, 0) = "Sue"
          saNames(2, 1) = "Thomas"
          saNames(3, 0) = "Jane"
          saNames(3, 1) = "Jones"
          saNames(4, 0) = "Adam"
          saNames(4, 1) = "Johnson"
        ' Fill A2:B6 with an array of values (First and Last Names).
          oSheet.Range("A2", "B6").Value = saNames
        ' Fill C2:C6 with a relative formula (=A2 & " " & B2).
          Set oRng = oSheet.Range("C2", "C6")
          oRng.Formula = "=A2 & "" "" & B2"
        ' Fill D2:D6 with a formula(=RAND()*100000) and apply format.
          Set oRng = oSheet.Range("D2", "D6")
          oRng.Formula = "=RAND()*100000"
          oRng.NumberFormat = "$0.00"
        ' AutoFit columns A:D.
          Set oRng = oSheet.Range("A1", "D1")
        ' Manipulate a variable number of columns for Quarterly Sales Data.
          Call DisplayQuarterlySales(oSheet)
        ' Make sure Excel is visible and give the user control
        ' of Microsoft Excel's lifetime.
          oXL.Visible = True
          oXL.UserControl = True
        ' Make sure you release object references.
          Set oRng = Nothing
          Set oSheet = Nothing
          Set oWB = Nothing
          Set oXL = Nothing
       Exit Sub
          MsgBox Err.Description, vbCritical, "Error: " & Err.Number
       End Sub
       Private Sub DisplayQuarterlySales(oWS As Excel.Worksheet)
          Dim oResizeRange As Excel.Range
          Dim oChart As Excel.Chart
          Dim iNumQtrs As Integer
          Dim sMsg As String
          Dim iRet As Integer
        ' Determine how many quarters to display data for.
          For iNumQtrs = 4 To 2 Step -1
             sMsg = "Enter sales data for" & Str(iNumQtrs) & " quarter(s)?"
             iRet = MsgBox(sMsg, vbYesNo Or vbQuestion _
                Or vbMsgBoxSetForeground, "Quarterly Sales")
             If iRet = vbYes Then Exit For
          Next iNumQtrs
          sMsg = "Displaying data for" & Str(iNumQtrs) & " quarter(s)."
          MsgBox sMsg, vbMsgBoxSetForeground, "Quarterly Sales"
        ' Starting at E1, fill headers for the number of columns selected.
          Set oResizeRange = oWS.Range("E1", "E1").Resize(ColumnSize:=iNumQtrs)
          oResizeRange.Formula = "=""Q"" & COLUMN()-4 & CHAR(10) & ""Sales"""
        ' Change the Orientation and WrapText properties for the headers.
          oResizeRange.Orientation = 38
          oResizeRange.WrapText = True
        ' Fill the interior color of the headers.
          oResizeRange.Interior.ColorIndex = 36
        ' Fill the columns with a formula and apply a number format.
          Set oResizeRange = oWS.Range("E2", "E6").Resize(ColumnSize:=iNumQtrs)
          oResizeRange.Formula = "=RAND()*100"
          oResizeRange.NumberFormat = "$0.00"
        ' Apply borders to the Sales data and headers.
          Set oResizeRange = oWS.Range("E1", "E6").Resize(ColumnSize:=iNumQtrs)
          oResizeRange.Borders.Weight = xlThin
        ' Add a Totals formula for the sales data and apply a border.
          Set oResizeRange = oWS.Range("E8", "E8").Resize(ColumnSize:=iNumQtrs)
          oResizeRange.Formula = "=SUM(E2:E6)"
          With oResizeRange.Borders(xlEdgeBottom)
             .LineStyle = xlDouble
             .Weight = xlThick
          End With
        ' Add a Chart for the selected data
          Set oResizeRange = oWS.Range("E2:E6").Resize(ColumnSize:=iNumQtrs)
          Set oChart = oWS.Parent.Charts.Add
          With oChart
             .ChartWizard oResizeRange, xl3DColumn, , xlColumns
             .SeriesCollection(1).XValues = oWS.Range("A2", "A6")
                For iRet = 1 To iNumQtrs
                   .SeriesCollection(iRet).Name = "=""Q" & Str(iRet) & """"
                Next iRet
             .Location xlLocationAsObject, oWS.Name
          End With
        ' Move the chart so as not to cover your data.
          With oWS.Shapes("Chart 1")
             .Top = oWS.Rows(10).Top
             .Left = oWS.Columns(2).Left
          End With
        ' Free any references.
          Set oChart = Nothing
          Set oResizeRange = Nothing
       End Sub
  6. Nh?n F5 ? ch?y cc d? n.


? bi?t thm thng tin v? vn ph?ng t? ?ng ha, xin vui l?ng truy c?p vo cc Pht tri?n vn ph?ng Microsoft h? tr? trang web t?i:
(c) t?p on Microsoft 1999, t?t c? cc quy?n. Nh?ng ng gp c?a Richard R. Taylor, Microsoft Corporation.

Thu?c tnh

ID c?a bi: 219151 - L?n xem xt sau cng: 21 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Office Excel 2007
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
T? kha:
kbexpertisebeginner kbautomation kbhowto kbmt KB219151 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:219151

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