|
楼主 |
发表于 2023-4-22 12:14
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 ning84 于 2023-4-23 18:23 编辑
- Function RetuSqlStr(FilePath, oAddress, oDate As Date)
- Dim Str
- Str = "Select "
- Str = Str & "a.日出,a.日落,"
- Str = Str & "b.日出,b.日落,"
- Str = Str & "c.日出,c.日落,"
- Str = Str & "d.日出,d.日落,"
- Str = Str & "e.日出,e.日落,"
- Str = Str & "f.日出,f.日落, "
- Str = Str & "g.日出,g.日落,"
- Str = Str & "h.日出,h.日落"
- Str = Str & " From "
- Str = Str & "[" & FilePath & "].[兰州$" & oAddress & "]a,"
- Str = Str & "[" & FilePath & "].[珠海$" & oAddress & "]b,"
- Str = Str & "[" & FilePath & "].[武汉$" & oAddress & "]c,"
- Str = Str & "[" & FilePath & "].[北京$" & oAddress & "]d,"
- Str = Str & "[" & FilePath & "].[抚远$" & oAddress & "]e,"
- Str = Str & "[" & FilePath & "].[漠河$" & oAddress & "]f, "
- Str = Str & "[" & FilePath & "].[三沙$" & oAddress & "]g, "
- Str = Str & "[" & FilePath & "].[喀什$" & oAddress & "]h"
- Str = Str & " Where "
- Str = Str & "a.日期 = #" & oDate & "# And "
- Str = Str & "b.日期 = #" & oDate & "# And "
- Str = Str & "c.日期 = #" & oDate & "# And "
- Str = Str & "d.日期 = #" & oDate & "# And "
- Str = Str & "e.日期 = #" & oDate & "# And "
- Str = Str & "f.日期 = #" & oDate & "# And "
- Str = Str & "g.日期 = #" & oDate & "# And "
- Str = Str & "h.日期 = #" & oDate & "# "
- ''
- RetuSqlStr = Str
- End Function
- Private Sub RecordsetSql()
- Dim Rng As Range
- Dim Str
- Dim FilePath, oAddress, oDate As Date
- FilePath = ThisWorkbook.Path & "\SunriseSunset.xlsx"
- oAddress = "A5:D996"
- oDate = "2022/3/3"
- Dim Cn As ADODB.Connection
- Dim Rs As ADODB.Recordset
- Set Cn = New ADODB.Connection
- Set Rs = New ADODB.Recordset
- Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
- Str = RetuSqlStr(FilePath, oAddress, oDate)
- Str = Replace(Str, "\Tmp", "")
- Debug.Print Str
- Set Rs = Cn.Execute(Str)
- Debug.Print Sheet3.Name
- With Sheet3
- .Cells.Clear
- .Cells.Font.Size = 9
- Set Rng = .Cells(2, 2)
- End With
- Rng.CopyFromRecordset Rs
- Rng.Resize(1000, Rs.fields.Count).NumberFormatLocal = "hh:mm;@"
- Stop
- Stop
- Stop
- End Sub
复制代码
|
|