问题
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