我需要一個訪問vba代碼來導入一個excel文件,但我需要用戶選擇哪個excel文件。我很新來訪問vba。訪問VBA代碼來導入excel文件
0
A
回答
0
0
我假設你需要給你更詳細一點你迄今爲止已經嘗試過。但是,從以下代碼開始可以提供幫助。
Function File_dailog() As String
On Error GoTo catchError
txtPath = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Dim directory As String, fileName As String, total As Integer
Dim fd As Object
Set fd = Application.FileDialog(3) ' this will open the file dailog
With fd ' following are the properties of the file dailog
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "Excel 2010", "*.xlsx?" ' you can add more filters of the file for the users below
If .Show = True Then
txtPath = Dir(.SelectedItems(1))
End If
txtPath = fso.GetFileName(.SelectedItems(1)) ' fetching the file name
End With
File_dailog = txtPath 'return value
exit_catchError:
Exit Function
catchError:
If Err.Number = 5 Then ' error handling if nothing selected by the user
Exit Function
End If
End Function
要導入Excel您可以使用:
strTable = "temp" ' name of table you want in your database
strFile = File_Dailog ' you will get the file name from user selection
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strFile, True
我希望這有助於。
0
這是我喜歡用的代碼:
Dim SelectedFile As String
Dim FilePicker As FileDialog
Dim SQLdelete As String
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
FilePicker.AllowMultiSelect = False
FilePicker.Filters.Add "Excel", "*.xls*", 1
FilePicker.InitialFileName = "C:\Users\"
FilePicker.Title = "Please Select the Excel Data..."
FilePicker.Show
If FilePicker.SelectedItems.Count <> 0 Then
SelectedFile = FilePicker.SelectedItems(1)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_Name", SelectedFile, True
MsgBox ("The data has been successfully loaded")
End If
注:tbl_Name將被保存在您希望的Excel數據表的名稱
相關問題
- 1. 訪問VBA代碼文件
- 2. 如何將csv文件導入到使用VBA代碼訪問?
- 3. 無法訪問Excel VBA代碼
- 4. 如何導入vba Excel代碼
- 5. 連接Excel來訪問 - VBA
- 6. 將excel文件導入訪問表
- 7. Excel公式來VBA代碼
- 8. VBA代碼導入
- 9. VBA訪問導出代碼模塊到文件夾
- 10. EXCEL VBA代碼來處理RightsWatch軟件
- 11. 訪問VBA從不同的子文件夾導入特定的Excel文件
- 12. VBA Excel Loop導入文本文件
- 13. 訪問vba將變量傳入代碼
- 14. 在Excel中使用VBA可以訪問VBA代碼嗎?
- 15. VBA - 從訪問生成Excel文件(QueryTable)
- 16. VBA Excel導入
- 17. Excel的VBA導入XML文件
- 18. Excel VBA代碼
- 19. 簡化訪問VBA代碼
- 20. Excel VBA代碼打開文件
- 21. VBA代碼 - 導入文本文件到Access表 - 隨着條件
- 22. 從excel文件複製頭文件(Excel VBA代碼)
- 23. 將VBA代碼應用於來自SSIS的Excel文件
- 24. 使用Excel Interop添加VBA代碼來優化文件
- 25. VBA導入最後修改的csv文件進入訪問
- 26. EXCEL VBA:訪問「Debug.Print」
- 27. 從ASP.NET文件夾訪問excel文件代碼隱藏
- 28. 導致excel滯後的VBA代碼
- 29. 對Excel VBA導入
- 30. VBA代碼來更新/創建新的記錄從Excel到訪
我不明白是什麼手段。你能給我一個故障嗎? – user3242868