|
楼主 |
发表于 2014-10-21 18:29
|
显示全部楼层
本帖最后由 ljd15366 于 2014-10-21 18:36 编辑
魂断蓝桥 发表于 2014-10-21 13:08
Sub a()
Dim cnn As Object, rs As Object, SQL$, d, Mypath$, MyName$, arr, brr(1 To 600, 1 To 2), i, ...
代码复制后运行时显示运行时错误‘91’,对象变量或with块变量未设置。能帮忙再改改吗,代码红色的部分有点问题
Sub a()
Dim cnn As Object, rs As Object, SQL$, d, Mypath$, MyName$, arr, brr(1 To 600, 1 To 2), i, m As Integer
Application.ScreenUpdating = False
Set d = CreateObject("Scripting.Dictionary")
Set rs = CreateObject("adodb.Recordset")
Mypath = ThisWorkbook.Path & "\"
MyName = Dir(Mypath & "*.xlsx")
Do While MyName <> ""
If MyName <> ThisWorkbook.Name Then
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyName
SQL = "select 学生编码,数学,化学 from [sheet1$a2:f] where 学生编码 is not null"
rs.Open SQL, cnn, 1, 1
If rs.RecordCount > 0 Then
arr = cnn.Execute(SQL).GetRows
For i = 0 To UBound(arr, 2)
m = m + 1
d(arr(0, i)) = m
brr(m, 1) = arr(1, i)
brr(m, 2) = arr(2, i)
Next
End If
End If
MyName = Dir()
rs.Close
Loop
cnn.Close
Set rs = Nothing
Set cnn = Nothing
arr = [a2].CurrentRegion
[a2:f999].ClearContents
For i = 2 To UBound(arr)
If Len(d(arr(i, 1))) > 0 Then
arr(i, 4) = brr(d(arr(i, 1)), 1)
arr(i, 6) = brr(d(arr(i, 1)), 2)
End If
Next
Set d = Nothing
[a2].Resize(UBound(arr), 6) = arr
Application.ScreenUpdating = True
End Sub |
|