|
此时的错误,我猜 只可能是L11及以下没内容 导致,所以容错可行。不容错也可如下(个人做法):
Option Explicit
Sub test()
Dim Cn As Object, Rs As Object, Sq$, p$, f$, ar, br, cr, i&
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Cn = CreateObject("ADODB.Connection")
p = ThisWorkbook.Path & "\"
ar = Range("E2:E" & Cells(Rows.Count, 5).End(xlUp).Row)
br = Range("L2:L" & Cells(Rows.Count, 5).End(xlUp).Row)
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=no';Data Source=" & ThisWorkbook.FullName
For i = 1 To UBound(ar)
f = Dir(p & ar(i, 1) & ".xls?")
If Len(f) Then
Sq = "SELECT f1 FROM [Excel 12.0;HDR=no;Database=" & p & f & "].[$l11:l] WHERE LEN(f1)"
Set Rs = Cn.Execute(Sq)
If Not Rs.EOF And Not Rs.BOF Then
cr = Rs.GetRows
If TypeName((cr(0, UBound(cr, 2)))) <> "String" Then br(i, 1) = Val(br(i, 1)) + cr(0, UBound(cr, 2))
End If
End If
Next
Range("L2").Resize(i - 1) = br
Cn.Close
Set Cn = Nothing
Set Rs = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Beep
End Sub |
评分
-
1
查看全部评分
-
|