|
数据源太不规范了,试试看吧:
Private Sub CommandButton1_Click()
Dim cnn As Object, SQL$, s$, s2$, Mypath$, MyFile$, i&
Set cnn = CreateObject("adodb.connection")
Mypath = ThisWorkbook.Path & "\"
MyFile = Mypath & "sourcetable.xls"
If Dir(MyFile) = "" Then
MsgBox "No File!"
Else
cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;hdr=no';Data Source=" & MyFile
s = "[Excel 12.0;Database=" & ThisWorkbook.FullName & "].[" & Me.Name & "$a1:a4]"
s2 = "SELECT distinct a.f1,a.f7 FROM [Overview$a9:g20] a," & s & " b where a.f1=b.Type"
SQL = "SELECT c.f7 FROM (" & s2 & ") c right join " & s & " d on c.f1=d.Type"
[b2].CopyFromRecordset cnn.Execute(SQL)
s = "[Excel 12.0;Database=" & ThisWorkbook.FullName & "].[" & Me.Name & "$a6:a49]"
s2 = "SELECT distinct a.f1,a.f9,a.f10,a.f12,a.f16 FROM [Data$b7:q55] a," & s & " b where a.f1=b.[Well No#]"
SQL = "SELECT c.f9,c.f10,c.f12,c.f16 FROM (" & s2 & ") c right join " & s & " d on c.f1=d.[Well No#]"
[b7].CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
End If
End Sub |
|