如何从excel文件导入特定的工作表?

时间:2013-12-20 03:27:45

标签: vba

我正在使用VBA Access 2010.显然,excel文件有13个工作表,我只需从一个工作表中获取列。而且,如果有一种方法可以跳过列标题,那将会很有帮助。谢谢。

以下是我正在处理的代码:

    stat.Caption = "Import Files"
    Me.Refresh

        If txtFileSource.Value <> "" Then

            Dim strTablename As String
            Dim xlsRpt As New Excel.Application
            Dim xbookRpt As New Excel.Workbook
            Dim iRowCountRpt As Integer
            Dim strRangeRpt As String

            If cmbData.Value = "My File" Then


                Set xbookRpt = Workbooks.Open(txtFileSource.Value)
                iRowCountRpt = xbookRpt.Sheets(1).UsedRange.Rows.Count
                strRangeRpt = "A1:I" & iRowCountRpt
                DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "tbl_Account", txtFileSource.Value, True, strRangeRpt
                xbookRpt.Close
                xlsRpt.Quit


            End If

        cmbData.Value = "Select File"
        txtDestination.Value = ""
        txtFileSource.Value = ""

        Call DeleteImportErrorTables
        Call MsgBox("Import successful!", vbInformation, "Import File")

    End If

stat.Caption = "Ready..."
Me.Refresh

1 个答案:

答案 0 :(得分:0)

试试这个

在定义范围时,您可以指定起始行 并且您可以使用结束行保持较大,因为Access会跳过空行

    DoCmd.TransferSpreadsheet acImport, , "myaccesstable", "myexceldocument", True, "myexcelsheet!A2:X1000"