|
With Rs
.MoveFirst
For ii = 0 To Rs.RecordCount - 1
''Debug.Print .Fields(4)
Set oRng = Sht.Cells(Rr + ii, "J")
SqlStr = "Select oName,oPath " & SqlShtStr & "Where oName ='" & .Fields(4) & "'"
Set oRs = SqlRetuRs(SqlStr)
With oRs
.MoveFirst
For jj = 0 To .RecordCount - 1
oRng(, jj) = .Fields(1)
Debug.Print oRng(, jj).Address, Format(Time - T, "h:mm:ss")
'oRng.Hyperlinks.Add oRng(, jj), .Fields(1)
.MoveNext
Next jj
End With
.MoveNext
Next ii
End With
循环 oRng(, jj) = .Fields(1),写到单元格中与1秒左右。
循环34710个Rs, 单元格中=34710/60^2,至少在9小时左右。
''''
请问高手运行下面的代码。用了多长时间。几秒钟能完成上百万的记录。
'''
- Function SqlRetuRs(SqlStr)
- Dim Cn As ADODB.Connection
- Set Cn = New ADODB.Connection
- Dim Rs As ADODB.Recordset
- Set Rs = New ADODB.Recordset
- '
- If InStr(UCase(Application.Path), "WPS") > 0 Then
- Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
- Else
- Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
- End If
- Rs.Open SqlStr, Cn, adOpenKeyset, adLockOptimistic
- Set SqlRetuRs = Rs
- End Function
- Function GroupYearMonthDay(SqlShtStr, Rng As Range, T As Date)
- ''"oDate", "Year", "YearMonth", "YearMonthDay", "oName", "oSize", "oType", "oPath"
- Dim Rs As ADODB.Recordset, oRs As ADODB.Recordset
- Dim SqlStr
- SqlStr = "Select Year, YearMonth, YearMonthDay,oDate, oName, oSize, oType,Count(oName) "
- 'SqlStr = "Select oDate, Year "
- SqlStr = SqlStr & SqlShtStr
- SqlStr = SqlStr & " Where Not oName Is Null And oType = 'JPG 文件' "
- SqlStr = SqlStr & "Group by oDate, Year, YearMonth, YearMonthDay, oName, oSize, oType "
-
- 'SqlStr = SqlStr & "Group by oDate, Year "
- SqlStr = SqlStr & " Order By oDate "
- Debug.Print SqlStr
- Dim tArr, Str, Rr
- Dim ii, jj
- tArr = Array("Year", "YearMonth", "YearMonthDay", "oDate", "oName", "oSize", "oType", "CountName", "oPath")
- Rng.Resize(, UBound(tArr)) = tArr
- Set Rs = SqlRetuRs(SqlStr)
- 'Debug.Print Rs.RecordCount, Rng(3, 1).Address
- Rng(3, 1).CopyFromRecordset Rs
- Dim Sht As Worksheet, oRng As Range
- Set Sht = Rng.Parent
- ''
- Rr = Rng.Row + 2
- With Sht
- .Cells(4, 2) = "=" & Rng.Resize(Rs.RecordCount + 1, Rs.Fields.Count + 1).Address
- Str = "=""Sql Gropy Date "" & " & "Count( " & Rng(3, "F").Resize(Rs.RecordCount + 1, 1).Address(0, 0) & ")"
- .Cells(2, "E") = Str & "&" & " "" ,Time:" & Format(Time - T, "h:mm:ss") & """"
-
- With Rs
- .MoveFirst
- For ii = 0 To Rs.RecordCount - 1
- ''Debug.Print .Fields(4)
- Set oRng = Sht.Cells(Rr + ii, "J")
- SqlStr = "Select oName,oPath " & SqlShtStr & "Where oName ='" & .Fields(4) & "'"
- Set oRs = SqlRetuRs(SqlStr)
- With oRs
- .MoveFirst
- For jj = 0 To .RecordCount - 1
- oRng(, jj) = .Fields(1)
- Debug.Print oRng(, jj).Address
- 'oRng.Hyperlinks.Add oRng(, jj), .Fields(1)
- .MoveNext
- Next jj
- End With
- .MoveNext
- Next ii
- End With
- Str = "=""Sql Gropy Date "" & " & "Count( " & Rng(4, "F").Resize(Rs.RecordCount + 1, 1).Address(0, 0) & ")"
- .Cells(3, "E") = Str & "&" & " "" ,Time:" & Format(Time - T, "h:mm:ss") & """"
- End With
-
- Stop
-
- Stop
- End Function
- Sub del()
- Dim Tt As Date: Tt = Time
- Dim Rng As Range
- Dim FolderRng As Range
- Dim AllFileRng As Range
- Dim Str
- Dim Rr, Cc
- Rr = 10
- Cc = 20
- Dim SqlShtStr
- With Sheet3
- SqlShtStr = " From [" & .Name & "$" & .Range(.Cells(4, 1).Formula).Address(0, 0) & "] "
- Debug.Print SqlShtStr
- GroupYearMonthDay " From [" & .Name & "$" & .Range(.Cells(4, 1).Formula).Address(0, 0) & "] ", .Cells(Rr, 1), Tt
- End With
- Stop
- End Sub
复制代码
|
|