Rem 全局变量 Dim xlApp As New Excel.Application Dim xlBook As New Excel.Workbook Dim my As New carClass Rem 对工作表的写操作 Private Sub Command1_Click() Rem 后期绑定 Rem Dim xlApp As Object Rem Set xlApp = CreateObject("Excel.Application") Set my = New carClass If Not IsNumeric(TextAge.Text) Then MsgBox "数据不对" End End If my.InAge = TextAge.Text my.StrName = TextName.Text Rem Dim xlSheet As New Excel.Worksheet Set xlApp = New Excel.Application Set xlBook = xlApp.Workbooks.add Rem xlApp.Workbooks.Open xlApp.Workbooks With xlApp.Sheets(1) .name = "test" .Range("a1") = "姓名" .Range("a2") = "年龄" .Range("b1") = my.StrName .Range("b2") = my.InAge End With xlApp.Visible = True xlBook.SaveAs "C:\Users\Administrator\Desktop\VISUAL\test2.xlsx" xlBook.Close Set xlApp = Nothing Set xlBook = Nothing Set my = Nothing End Sub Rem 对工作表读取操作 Private Sub Command2_Click() Set xApp = New Excel.Application Set xBook = xApp.Workbooks.Open("C:\Users\Administrator\Desktop\VISUAL\test2.xlsx") MsgBox xApp.Sheets(1).Range("a2").value Set xApp = Nothing Set xBook = Nothing End Sub Private Sub Command3_Click() Dim x As Integer, y As Integer x = 2: y = 3 MsgBox add(x, y) End Sub
来源:https://www.cnblogs.com/lixiaomingr/p/11370542.html