通过Excel SQL查询使用偏移量

我正在从一个Excel文件收集来自他人的信息closures的Excel文件提供商是Microsoft.ACE.OLEDB.12.0和一切工作正常(几乎)。

为了具有可更新的查询,我使用了命令HDR = no ,以使列名称如F1,F2,F3 …和我检索后的名称(请参阅下面的代码,从堆栈溢出代码)。

然而,使用Union All命令,我也检索了标题作为数据,如果我从5个文件收集数据,我会得到5个标题。 所以我正在寻找一个解决scheme来检索标题与Excel SQL查询命令HDR = NO (从每个文件的第2行开始)。

我在SQL查询中尝试OFFSET命令,但我收到一条错误消息。 我也尝试获取原始文件中的行号,但没有find该命令。

你有什么想法来帮助我解决这个问题吗?

非常感谢,BR

信息代码:

 Option Explicit Sub SqlUnionTest() Dim strConnection As String Dim strQuery As String Dim objConnection As Object Dim objRecordSet As Object strConnection = _ "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "User ID=Admin;" & _ "Data Source='" & ThisWorkbook.FullName & "';" & _ "Mode=Read;" & _ "Extended Properties=""Excel 12.0 Macro;"";" strQuery = _ "SELECT * FROM [Sheet1$] " & _ "IN '" & ThisWorkbook.Path & "\Source1.xlsx' " & _ "[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=NO;'] " & _ "UNION " & _ "SELECT * FROM [Sheet1$] " & _ "IN '" & ThisWorkbook.Path & "\Source2.xlsx' " & _ "[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=NO;'] " & _ "UNION " & _ "SELECT * FROM [Sheet1$] " & _ "IN '" & ThisWorkbook.Path & "\Source3.xlsx' " & _ "[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=NO;'] " & _ "ORDER BY ContactName;" Set objConnection = CreateObject("ADODB.Connection") objConnection.Open strConnection Set objRecordSet = objConnection.Execute(strQuery) RecordSetToWorksheet Sheets(1), objRecordSet objConnection.Close End Sub Sub RecordSetToWorksheet(objSheet As Worksheet, objRecordSet As Object) Dim i As Long With objSheet .Cells.Delete For i = 1 To objRecordSet.Fields.Count .Cells(1, i).Value = objRecordSet.Fields(i - 1).Name Next .Cells(2, 1).CopyFromRecordset objRecordSet .Cells.Columns.AutoFit End With End Sub