我在网上找了一段VB操作Excle的代码
Private Sub Command1_Click()
'引用 Microsoft Excel 11.0 Object Library
Dim xlsApp As Excel.Application
Dim eworkbook As Workbook
Dim eworksheet As Worksheet
Set xlsApp = New Excel.Application
Set eworkbook = xlsApp.Workbooks.Open( "D:\sp.xls ")
Set eworksheet = eworkbook.Sheets( "Sheet1 ")
With eworksheet
'1
.Cells(1, 1) = "1 "
.Cells(1, 2) = "2 "
.Cells(1, 3) = "3 "
'2
.Cells(2, 1) = "11 "
.Cells(2, 2) = "22 "
.Cells(2, 3) = "33 "
'3
.Cells(3, 1) = "111 "
.Cells(3, 2) = "222 "
.Cells(3, 3) = "333 "
End With
eworkbook.Save
eworkbook.Close
xlsApp.Quit
End Sub
但是我一点运行就提示 用户定义类型未定义,请问各位大侠,怎么办啊
Private Sub Command1_Click()
'引用 Microsoft Excel 11.0 Object Library
Dim xlsApp As Excel.Application
Dim eworkbook As Workbook
Dim eworksheet As Worksheet
Set xlsApp = New Excel.Application
Set eworkbook = xlsApp.Workbooks.Open( "D:\sp.xls ")
Set eworksheet = eworkbook.Sheets( "Sheet1 ")
With eworksheet
'1
.Cells(1, 1) = "1 "
.Cells(1, 2) = "2 "
.Cells(1, 3) = "3 "
'2
.Cells(2, 1) = "11 "
.Cells(2, 2) = "22 "
.Cells(2, 3) = "33 "
'3
.Cells(3, 1) = "111 "
.Cells(3, 2) = "222 "
.Cells(3, 3) = "333 "
End With
eworkbook.Save
eworkbook.Close
xlsApp.Quit
End Sub
但是我一点运行就提示 用户定义类型未定义,请问各位大侠,怎么办啊
找到 Microsoft Excel 11.0 Object Library
'引用 Microsoft Excel 11.0 Object Library
Dim xlapp As Variant
Dim xlsApp As Variant ' Excel.Application
Dim eworkbook As Variant
Dim eworksheet As Variant
Set xlsApp = CreateObject("excel.application")
Set eworkbook = xlsApp.Workbooks.Open("D:\sp.xls")
Set eworksheet = eworkbook.Sheets("Sheet1")
With eworksheet
'1
.Cells(1, 1) = "1 "
.Cells(1, 2) = "2 "
.Cells(1, 3) = "3 "
'2
.Cells(2, 1) = "11 "
.Cells(2, 2) = "22 "
.Cells(2, 3) = "33 "
'3
.Cells(3, 1) = "111 "
.Cells(3, 2) = "222 "
.Cells(3, 3) = "333 "
End With
eworkbook.Save
eworkbook.Close
xlsApp.Quit
End Sub