我有33個鏈接的表全部位於同一個目錄(共享點站點)。我想要一些代碼複製到此目錄中的所有表中,並創建此數據的統一表格通過ms-access合併目錄中的文件VBA
所有表格都是相同的格式並且位於同一文件夾中。
我曾嘗試聯盟和其他方式,但這些已被限制在我可以使用的列數。
Excel模板中的列高達GG
我有33個鏈接的表全部位於同一個目錄(共享點站點)。我想要一些代碼複製到此目錄中的所有表中,並創建此數據的統一表格通過ms-access合併目錄中的文件VBA
所有表格都是相同的格式並且位於同一文件夾中。
我曾嘗試聯盟和其他方式,但這些已被限制在我可以使用的列數。
Excel模板中的列高達GG
您可以嘗試使用通過目錄Dir
循環,並使用DoCmd.TransferSpreadsheet
導入電子表格在特定的目錄中。將「YourTable」替換爲您的表名稱,將「\ yourdirectory \」替換爲SP文件夾的位置。
Sub ExcelImport()
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("\\yourdirectory\")
While (file <> "")
If InStr(file, "xls") Or InStr(file, "xlsx") > 0 Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "YourTable", file, True
End If
file = Dir
Wend
End Sub
我會做類似的事情,到瑞安提到的,而是直接到表不會導入我想查詢(主要是因爲有可能是家政服務和/或數據質量問題,你的負荷可能需要先處理)。
我首先創建一個具有多個字母數字字段的登臺表(GG會帶你到189個字段;我會稱他們爲F1-F189),加上一個字段來存儲文件名(也許還有一些額外的字段處理避免重複記錄或處理表內更新 - 日期時間戳和進程標誌)。
然後,您可以有一個循環,將每個文件依次導入到登臺表中,然後在循環內執行查詢(指的是預先寫好的Access查詢是最好的,因爲您需要設置一個參數爲處理文件名;另外,您可能需要隨時調整它),它將使用FOR-NEXT循環中的文件名將登臺表中的記錄插入到適當的目的地表中。
在循環中,導入每個文件之後,您可能需要執行一些內務操作,然後再轉到下一個文件。
下面是我從我使用的東西中非常粘貼的東西 - 它很混亂,它確實使用了從別人的代碼改編的位,所以我會相信,在那裏我記得並且事先爲那些人道歉我沒有:
Sub import_ASSGMU()
' Macro loops through the specified directory (strPath)
' and imports Excel files to staging table in the Access Database
' (tbl_import_generic)
' before using an Access query to insert into the cleaned-up destination table
' (tbl_transformed_ASSGMU)
Dim strPath As String
strPath = "c:\data" ' YOU MUST SET YOUR OWN PATH HERE
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
' this bit is if you want to move files out of the folder after they are processed
' Dim ImportSuccessPath As String
' ImportSuccessPath = "c:\data\Processed" 'hard-coded sub-folder name
' If Right(ImportSuccessPath, 1) <> "\" Then ImportSuccessPath = ImportSuccessPath & "\"
Dim strFile As String 'Filename
Dim strFileList() As Variant 'File Array
Dim intFile As Integer 'File Number
'Loop through the folder & build file list - file name prefix is HARDCODED
strFile = Dir(strPath & "ASSG_MU_*.xls*")
' this takes all the filenames and adds them to an array
While strFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
'see if any files were found
If intFile = 0 Then
'MsgBox "No files found"
Exit Sub
End If
' sorts the array so files are in filename order
' needs a procedure from here:
' http://www.access-programmers.co.uk/forums/showthread.php?t=194737
Call SortArray(strFileList())
' cycle through the list of files in the array & import to Access
For intFile = 1 To UBound(strFileList)
' empty staging table
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE FROM tbl_import_generic;"
DoCmd.SetWarnings True
' delete records from same filename previously imported
'DoCmd.SetWarnings False
' DoCmd.RunSQL "DELETE FROM tbl_transformed_ASSGMU WHERE import_file = """ & strFileList(intFile) & """;"
'DoCmd.SetWarnings True
' allows import of xls AND xlsx
If Right(strFileList(intFile), 1) = "s" Then
DoCmd.TransferSpreadsheet acImport, 8, "tbl_import_generic", strPath & strFileList(intFile), False, "A1:GG50000"
ElseIf Right(strFileList(intFile), 1) = "x" Then
DoCmd.TransferSpreadsheet acImport, 10, "tbl_import_generic", strPath & strFileList(intFile), False, "A1:GG50000"
End If
'DoCmd.Echo False, ""
DoCmd.SetWarnings False
Set db = CurrentDb
Dim qdf As DAO.QueryDef
' create an Access INSERT query with a name that matches the next code line that will take records from the tbl_import_generic and write them to the destination table. At this stage you can embed some hygeine factors or parse data in the Access query
Set qdf = db.QueryDefs("qry_transform_ASSGMU")
' create a Parameter called param_filename and use it to populate a
' destination field - this will then put the name of the imported
' file in your destination table
qdf!param_filename = strFileList(intFile)
qdf.Execute 'dbFailOnError ' useful when testing your query
DoCmd.SetWarnings True
' if you want to "move" files out of the folder when processed
' copy processed file to Processed folder & delete file from Import
' FileCopy strPath & strFileList(intFile), ImportSuccessPath & strFileList(intFile)
' Kill strPath & strFileList(intFile)
Next
MsgBox UBound(strFileList) & " file(s) were imported."
End Sub
有一個玩弄它。希望這對你有用。
請顯示您嘗試過的內容,輸出以及您希望輸出的內容。 – Matt