Can you import only the 2nd Sheet from Excel to Access using VBA

笑着哭i 提交于 2020-04-12 06:12:08

问题


Can you import only the 2nd Sheet from Excel to Access using VBA?! I did some Googling on this and couldn't find a definitive answer.

Here is the code that I'm trying to get working.

strPathFile = Fil

objXL.Visible = True
Set wkb = objXL.Workbooks.Open(strPathFile)
For Each wks In wkb.Worksheets
    If wkb.wks = wkb.Sheet(2) Then
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames
    End If
Next

I'm trying to import data from a bunch of Excel files, always from the second sheet in the file, but each sheet has a different name.


回答1:


As it turns out, I was missing 2 things.

1)  wks.Index = 2

2)  NeedThisSheet = wks.Name & "!"

Here is the code snippit.

strPathFile = Fil

objXL.Visible = True
Set wkb = objXL.Workbooks.Open(strPathFile)
For Each wks In wkb.Worksheets
    If wks.Index = 2 Then
        NeedThisSheet = wks.Name & "!"
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames, NeedThisSheet
    End If
Next
wkb.Close

Thanks everyone!!



来源:https://stackoverflow.com/questions/45312837/can-you-import-only-the-2nd-sheet-from-excel-to-access-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!