代码之家  ›  专栏  ›  技术社区  ›  Vinnie Novido

用VBA中的SQL查询导入excel数据将跳过第一行

  •  1
  • Vinnie Novido  · 技术社区  · 6 年前

    也许你们中的一个能发现我的错误或者向我解释为什么会这样?

    Sub ImportExcelSQL()
    
    Dim sheetName, sheetNewName, filepath, strConnection, Sql As String
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    '-------- Close workbook updates ----------
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
    Application.StatusBar = "Importing...."
    '------------------------------------------
    
    filepath = Range("filepath")
    
    strConnection = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" _
                      & "DBQ=" + filepath + ";"
    
    ' Create the Connection and Recordset objects.
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    ' Open connection
    conn.Open strConnection
    
    ' Loop through the sheets
    Dim i As Integer
    i = 1
    Do Until IsEmpty(Range("importSheetNames").Offset(i, 0))
    
        If Range("importSaveSheetFlags").Offset(i, 0).Value = "Y" Then
    
        ' Get sheet names and input variables"
        sheetName = Range("importSheetNames").Offset(i, 0).Value
        sheetNewName = Range("exportSheetNames").Offset(i, 0).Value
        filepath = Range("filepath")
    
        ' Clear data sheet
        Sheets(sheetNewName).UsedRange.ClearContents
    
        ' ----------------------- SQL CODE ----------------------------
        Sql = "SELECT * FROM [" + sheetName + "$A:CA]"
        'Sql = "SELECT * FROM [" + sheetName + "$A1:CA1000]" 'Does not do any difference
    
        ' Open the connection and execute.
        'conn.Open strConnection
        Set rs = conn.Execute(Sql)
    
        ' Check we have data.
        If Not rs.EOF Then
           ' Transfer result.
           Sheets(sheetNewName).Range("A1").CopyFromRecordset rs
           ' Close the recordset
           rs.Close
        Else
           MsgBox "Error: No records returned.", vbCritical
        End If
    
        ' -------------- End of SQL --------------------------------------------
    
        End If
    
    
        i = i + 1
    Loop
    
    ' Clean up
    If CBool(conn.State And adStateOpen) Then conn.Close
    Set conn = Nothing
    Set rs = Nothing
    
    '-----------------------------------------------
    ' Turn on automatic updating
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
    Application.StatusBar = "Finished"
    '-----------------------------------------------
    
    End Sub
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   FunThomas    6 年前

    理论上,连接字符串中有一个参数,您可以在其中定义是否有标题行, HDR=YES; ,但是对于这个驱动程序,似乎忽略了这个参数,而是从注册表中读取了一个值。看到了吗 https://stackoverflow.com/a/49555650/7599798

    作为替代方法,您可以使用OLE驱动程序:Try

    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filepath _ 
                  & ";Extended Properties=""Excel 12.0 Xml;HDR=NO;"""
    

    这符合 HDR ,所以如果你写 HDR=NO ,它将复制第一行,而 HDR=YES 跳过它。如果您有标题行,则可以在 SQL