Private Sub Command0_Click()
Dim xlapp As Object, xlbook As Object, sheetName As String, i As Long, j As Long
Dim F
Set xlapp = CreateObject("excel.application")
With Application.FileDialog(3)
.AllowMultiSelect = True
.Filters.Clear
.Filters.Add "Excel文件", "*.xls;*.xlsx"
If .Show Then
For Each F In .SelectedItems
Set xlbook = xlapp.Workbooks.Open(F, , True) '打开Excel文件
sheetName = "" '针对每个Excel文件,初始化一下
For i = 1 To xlbook.Sheets.Count '循环所有的sheet
'按照某些固有的特征判断sheet是不是要导入的数据表
If xlbook.Sheets(i).Cells(1, 1) = "仓库" And xlbook.Sheets(i).Cells(1, 2) = "id" Then '特征判断
sheetName = xlbook.Sheets(i).Name '确定为要导入的数据表的名称
Exit For '既然找到了就退出循环
End If
Next i '没有找到,就继续,一直到循环完成
xlbook.Close '你打开了他,就一定要关闭它.
If sheetName = "" Then '循环完成后,也没有找到要导入的数据表.
Debug.Print "文件[" & F & "]中没有你要的数据!"
Else '已经找到要导入的数据表
CurrentDb.Execute "INSERT INTO SHEET1 (ID,仓库) select ID,仓库 from [Excel 8.0;Database=" & F & "].[" & sheetName & "$]"
j = j + 1
End If
Next
End If
End With
MsgBox "一共导入了[" & j & "]张表的数据!"
End Sub
以上代码基于Excel的A1单元格为“仓库”,B1单元格为“id”的情况来确定就是你要导入的sheet。当然,你还可以添加更多的判断(假如还有其他字段,并且字段顺序有定数的话更好),来确认是不是你要导入的sheet