|
楼主 |
发表于 2024-5-30 12:17
|
显示全部楼层
复杂的事,SQL简单化了。
这个代码,还有很大的优化空间。
- Function SqlRetuRs(Str)
- 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 Str, Cn, adOpenKeyset, adLockOptimistic
- Set SqlRetuRs = Rs
- End Function
- Sub DELDELDEL()
- Dim RootName
- RootName = "StreetSnap"
- Dim Rng As Range, oRng As Range
- Set Rng = Selection
- Dim oDate As Date
- 'oDate = Selection
-
- Dim Sht As Worksheet
- Set Sht = Rng.Parent
- Set Rng = Sht.Range(Sht.Cells(4, 1).Formula)
- Debug.Print Address
- Dim Str, Rs As ADODB.Recordset
- Dim Rr
- Rr = 8
- 'Str = "select Distinct '\' + format(oDate,'yyyy年'),"
-
- 'Str = Str & "'\' + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月'),"
- Str = "select Distinct "
- Str = Str & "'" & RootName & "\'"
- Str = Str & " + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月') + '\' + format(oDate,'yyyy年mm月dd日') + '\' , "
- Str = Str & "Name From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Where Name like '%jpg'"
- '
- Str = "select Distinct format(oDate,'yyyy年mm月dd日 hh:mm:ss'),Name,"
- Str = Str & "'" & RootName & "\'"
- Str = Str & " + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月') + '\' + format(oDate,'yyyy年mm月dd日') + '\' + Name "
- Str = Str & " From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Where Name like '%jpg'"
-
- Debug.Print Str
-
- Str = "select Distinct "
- Str = Str & "'" & RootName & "\' + format(oDate,'yyyy年')"
- 'Str = Str & " + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月') + '\' + format(oDate,'yyyy年mm月dd日') + '\' + Name "
- Str = Str & " From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Where Name like '%jpg'"
-
- Debug.Print Str
-
-
-
- Set Rs = SqlRetuRs(Str)
- Set oRng = Sht.Cells(Rr, 10)
- oRng.CopyFromRecordset Rs
-
- Str = "select Distinct "
- Str = Str & "'" & RootName & "\' + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月') + '\' "
- 'Str = Str & " + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月') + '\' + format(oDate,'yyyy年mm月dd日') + '\' + Name "
- Str = Str & " From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Where Name like '%jpg'"
-
- Debug.Print Str
-
-
-
- Set Rs = SqlRetuRs(Str)
- Set oRng = Sht.Cells(Rr, 11)
- oRng.CopyFromRecordset Rs
-
- Str = "select Distinct "
- 'Str = Str & "'" & RootName & "\' + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月') + '\' "
- Str = Str & " + format(oDate,'yyyy年') + '\' + format(oDate,'yyyy年mm月') + '\' + format(oDate,'yyyy年mm月dd日') + '\' "
- Str = Str & " From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Where Name like '%jpg'"
-
- Debug.Print Str
-
-
-
- Set Rs = SqlRetuRs(Str)
- Set oRng = Sht.Cells(Rr, 12)
- oRng.CopyFromRecordset Rs
-
-
- End Sub
复制代码
|
|