Вы должны включить множественный выбор.попробуйте следующий код, чтобы связать или импортировать файлы, а затем объединить их:
Sub Importer()
Dim fDialog As Office.FileDialog
Dim FileName As Variant
Dim TableName As String
Dim TableCnt As Integer
Dim FileFlag As Integer
'......... File Dialog ............
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = True
.Title = "Select KPI csv files."
.Filters.Add "MY FILE TYPES", "*.csv;*.xls;*.xlsx", 1
.FilterIndex = 1
.InitialFileName = Environ("userprofile") & "\Desktop\Q3\"
If .Show = False Then
Exit Sub
End If
End With
'............ Import files ................
DoCmd.SetWarnings False
For Each FileName In fDialog.SelectedItems
Select Case Right(FileName, 4)
Case ".csv"
FileFlag = CheckCSVFileType(CStr(FileName))
If FileFlag > 0 Then
'... set first row of importing csv file.
'... You should create an importing specification then go to navigation pane, set settings
'... to show system objects, then find MSysIMEXSpecs hidden table.
'... Your defined specifications settings are there.
'... find specID for your csv importing specification,
'... and change 6666 in the bellow to that number.
DoCmd.RunSQL ("UPDATE " & _
"MSysIMEXSpecs SET MSysIMEXSpecs.StartRow =" & FileFlag & _
" WHERE (((MSysIMEXSpecs.SpecID)=6666)); ")
'... Linking or importing file
DoCmd.TransferText _
acLinkDelim, _
"YourSpecificationName", _
"Table Name in access(will be merged at the end)", _
FileName, _
True
End If
Case ".xls", "xlsx"
ImportXLSFileType CStr(FileName)
End Select
Next FileName
DoCmd.SetWarnings True
End Sub
'.. This Function Check text file and search 10 first row to find special string which shows your data header.
'.. then return row number of heading row. If no such row found in first 10 rows, return -1.
Function CheckFileType(FileName As String) As Integer
Dim DataStr As String
Dim BlankCheck As Integer
Open FileName For Input Access Read As #1
BlankCheck = 0
CheckFileType = -1
Do
BlankCheck = BlankCheck + 1
Line Input #1, DataStr
If InStr(1, DataStr, "Your expected string Or part of your expected header") > 0 Then
CheckFileType = BlankCheck
End If
Loop While Not EOF(1) And BlankCheck < 10 And CheckFileType = -1
Close #1
End Function
Sub ImportXLSFileType(FileName As String)
Dim DataSheet As Worksheet
Dim DataBook As Workbook
Dim LastCell As String
Dim FR As Range
Dim DataRange As String
Dim DelRow As Integer
Set DataBook = Workbooks.Open(FileName, 0, False)
DataBook.Application.WindowState = xlMinimized
For Each DataSheet In DataBook.Worksheets
With DataSheet
Set FR = .Range("1:5").Find(what:="BTSNAME", lookat:=xlWhole)
If Not FR Is Nothing Then
DoCmd.TransferSpreadsheet _
acLink, _
acSpreadsheetTypeExcel12Xml, _
"Your table name in access", _
FileName, _
True, _
.Name & FR.Address & ":" & .Range("A" & .cells.Rows.Count).End(xlTop).End(xlRight).Address
DoCmd.RunSQL "INSERT INTO [Importing Files] (FilePath, SheetName, Range, FileType) SELECT """ & _
FileName & """,""" & .Name & """,""" & DataRange & """," & hka2Gxls & ";"
End If
End With
Next
End Sub