|
3#
楼主 |
发表于 2012-8-22 14:12:12
|
只看该作者
本帖最后由 Benjamin_luk 于 2012-8-22 14:12 编辑
但是出现问题了,
在用TARGETR进行赋值,提示错误"需要对象"
我想是因为EXCEL已关闭的原因, 那就将TARGETR转到另一个变量,测试成功!
速度比TransferSpreadsheet要快得多了, 最后代码如下:
红色为新增加的变量
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim rs As Recordset
Dim I As Double, J As Integer, n As Integer
Dim TargetR, stime, DataK
If Dir(filstr) <> "" Then
stime = Timer
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(filstr)
Set xlSheet = xlBook.Worksheets(1)
CurrentDb.Execute "Delete * from [1 库存(MB52)]"
Set rs = CurrentDb.OpenRecordset("1 库存(MB52)")
'MsgBox rs.RecordCount
xlApp.ScreenUpdating = False
With xlSheet
I = .Range("A1").End(xlDown).Row
Set TargetR = .Range("A1:I" & I)
xlApp.ScreenUpdating = True
End With
DataK = TargetR
xlApp.Workbooks.Close
xlApp.Quit
Set xlApp = Nothing
For J = 2 To I
rs.AddNew
rs.Fields(1) = DataK(J, 1)
rs.Fields(2) = DataK(J, 2)
rs.Fields(3) = DataK(J, 3)
rs.Fields(4) = DataK(J, 4)
rs.Fields(5) = DataK(J, 5)
rs.Fields(6) = DataK(J, 6)
rs.Fields(7) = DataK(J, 7)
rs.Fields(8) = DataK(J, 8)
rs.Update
Next
MsgBox "成功导入库存资料" & Round(Timer - stime, 0), vbInformation
Else
MsgBox "库存文件不存在", vbCritical, "请检查库存文件路径"
End If
Set rs = Nothing |
|