|
Option Explicit
Sub 查询()
Dim cnn As Object, MYF$, n%
Dim Sql As String
Set cnn = CreateObject("ADODB.CONNECTION")
MYF = Dir(ThisWorkbook.Path & "\Rawdata\*.xlsx")
Do While MYF <> ""
n = n + 1
If n = 1 Then
cnn.Open "Provider=Microsoft.ACE.OleDb.12.0;Extended Properties='Excel 12.0;HDR=YES'; Data Source=" & ThisWorkbook.Path & "\Rawdata\" & MYF
Sql = "SELECT 姓名,年龄,性别,学号,成绩,""" & Left(MYF, Len(MYF) - 5) & """ FROM [Sheet1$a1:e] WHERE 姓名 IS NOT NULL"
Else
Sql = Sql & " UNION ALL SELECT 姓名,年龄,性别,学号,成绩,""" & Left(MYF, Len(MYF) - 5) & """ FROM [Excel 12.0;Database=" & ThisWorkbook.Path & "\Rawdata\" & MYF & "].[Sheet1$a1:e] WHERE 姓名 IS NOT NULL "
End If
MYF = Dir()
Loop
[a2:f999] = ""
[a2].CopyFromRecordset cnn.Execute(Sql)
cnn.Close: Set cnn = Nothing
End Sub |
|