|
本帖最后由 suye1010 于 2012-7-6 11:52 编辑
以下的代码可以正确的运行并获得结果:
- Sub UpdateXlsFileForUpload()
- Dim cnn, SQL As String, myData As String, wb As Workbook
- myData = ThisWorkbook.Path & "\Off Take Tire Information.mdb"
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "provider=microsoft.ace.oledb.12.0;data source=" & myData
- Set wb = Application.Workbooks.Open(ThisWorkbook.Path & "\Reports\Off Take Tire information-Notes Database.xls")
- With wb
- SQL = "SELECT * FROM [Technical Information-Summary] Where [Supplier] is not null Order By [supplier],[Tread Pattern],[True Brand],[Status],[Size]"
- If Not .Sheets("Technical Information").ListObjects("TechnicalInformation").DataBodyRange Is Nothing Then .Sheets("Technical Information").ListObjects("TechnicalInformation").DataBodyRange.Delete
- .Sheets("Technical Information").Cells(4, 1).Offset(1, 0).CopyFromRecordset cnn.Execute(SQL)
- If Not .Sheets("Update History").ListObjects("UpdateHistory").DataBodyRange Is Nothing Then .Sheets("Update History").ListObjects("UpdateHistory").DataBodyRange.Delete
- .Sheets("Update History").Cells(2, 1).CopyFromRecordset cnn.Execute("SELECT * FROM [Technical Information-Update History]")
- .Close SaveChanges:=False
- End With
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 但当使用以下代码,在运行到第10行,显示数据更新了,但是excel被莫名关闭,后续代码无法执行,再次打开要输入的文件也会发现未被保存
- Sub UpdateXlsFileForUpload()
- Dim cnn, SQL As String, myData As String, wb As Workbook, app as New Application
- myData = ThisWorkbook.Path & "\Off Take Tire Information.mdb"
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "provider=microsoft.ace.oledb.12.0;data source=" & myData
- Set wb = app.Workbooks.Open(ThisWorkbook.Path & "\Reports\Off Take Tire information-Notes Database.xls")
- With wb
- SQL = "SELECT * FROM [Technical Information-Summary] Where [Supplier] is not null Order By [supplier],[Tread Pattern],[True Brand],[Status],[Size]"
- If Not .Sheets("Technical Information").ListObjects("TechnicalInformation").DataBodyRange Is Nothing Then .Sheets("Technical Information").ListObjects("TechnicalInformation").DataBodyRange.Delete
- .Sheets("Technical Information").Cells(4, 1).Offset(1, 0).CopyFromRecordset cnn.Execute(SQL)
- If Not .Sheets("Update History").ListObjects("UpdateHistory").DataBodyRange Is Nothing Then .Sheets("Update History").ListObjects("UpdateHistory").DataBodyRange.Delete
- .Sheets("Update History").Cells(2, 1).CopyFromRecordset cnn.Execute("SELECT * FROM [Technical Information-Update History]")
- .Close SaveChanges:=False
- End With
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码
|
|