|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub test_Sql() '//2022/10/1
Dim Cnn, Rs, StrPath$, StrC$, StrSQL$, i&
Set Cnn = CreateObject("adodb.connection")
StrPath = ThisWorkbook.FullName
If Application.Version < 12 Then
StrC = "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & StrPath
Else
StrC = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & StrPath
End If
Cnn.Open StrC
ar = Sheet1.[A1].CurrentRegion.Rows(1)
ar = Application.Transpose(Application.Transpose(ar))
For i = 1 To UBound(ar)
S = S & ",[" & ar(i) & "] "
Next i
S = Mid(S, 2, Len(S) - 1)
StrSQL = "select " & S & "from [" & Sheet2.Name & "$]"
Set Rs = Cnn.Execute(StrSQL)
With Sheet1
.[A1].CurrentRegion.Offset(1).Clear
.[A2].CopyFromRecordset Rs
With .[A1].CurrentRegion
.Borders.LineStyle = 1
.Font.Size = 11
End With
End With
Cnn.Close
Set Cnn = Nothing
MsgBox "OK!"
End Sub
|
评分
-
1
查看全部评分
-
|