Export tung sheets trong 1 file Excel vào thành từng bảng trong ACCESS (3 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

hoquang1987

Thành viên mới
Tham gia
17/9/09
Bài viết
3
Được thích
3
Hi các members,Mình có một file excel và 1 file access.File excel có rất nhiều sheets, và mình muốn export thành từng table trong access có tên tương ứng theo từng sheets.Bình thường mình sử dụng: import từ access để import từng sheet trong excel. Việc này tốn rất nhiều thời gian.Các members có thể giúp mình solve problem này đc không?Many thanks and best regards,Quang
 
Lần chỉnh sửa cuối:
Hi các members,Mình có một file excel và 1 file access.File excel có rất nhiều sheets, và mình muốn export thành từng table trong access có tên tương ứng theo từng sheets.Bình thường mình sử dụng: import từ access để import từng sheet trong excel. Việc này tốn rất nhiều thời gian.Các members có thể giúp mình solve problem này đc không?Many thanks and best regards,Quang
Bạn dùng code sau nhé:
Mã:
Sub Import()
Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim strValue As String
On Error GoTo Import_Error
Set appExcel = CreateObject("Excel.Application")
Set wb = appExcel.Workbooks.Open[COLOR=red]("C:\Users\Le Phat Dom\Desktop\Login\A.xls")[/COLOR]
For Each sh In wb.Sheets
Debug.Print sh.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_" & sh.Name, _
[COLOR=red]"C:\Users\Le Phat Dom\Desktop\Login\A.xls",[/COLOR] True, sh.Name & "!"
Next
wb.Close
appExcel.Quit
   On Error GoTo 0
   Exit Sub
Import_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
End Sub

Tôi giả sử file bạn muốn nhập có tên là A và nằm trong đường dẫn "C:\Users\Le Phat Dom\Desktop\Login\A.xls", Bạn phải sửa lại đường dẫn cho đúng thực tế nhé.
 
Bạn dùng code sau nhé:
Mã:
Sub Import()
Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim strValue As String
On Error GoTo Import_Error
Set appExcel = CreateObject("Excel.Application")
Set wb = appExcel.Workbooks.Open[COLOR=red]("C:\Users\Le Phat Dom\Desktop\Login\A.xls")[/COLOR]
For Each sh In wb.Sheets
Debug.Print sh.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_" & sh.Name, _
[COLOR=red]"C:\Users\Le Phat Dom\Desktop\Login\A.xls",[/COLOR] True, sh.Name & "!"
Next
wb.Close
appExcel.Quit
   On Error GoTo 0
   Exit Sub
Import_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
End Sub

Tôi giả sử file bạn muốn nhập có tên là A và nằm trong đường dẫn "C:\Users\Le Phat Dom\Desktop\Login\A.xls", Bạn phải sửa lại đường dẫn cho đúng thực tế nhé.

Thanks bạn nhiều, Mình đã làm được rồi, but
bạn có thể giúp mình làm động tác ngược lại không?
có nghĩa là mình có 1 file access có nhiều bảng và mình muốn export ra thàng từng sheet riêng biệt trong 1 file excel.

Thanks and regards,
Quang
 
Thanks bạn nhiều, Mình đã làm được rồi, but
bạn có thể giúp mình làm động tác ngược lại không?
có nghĩa là mình có 1 file access có nhiều bảng và mình muốn export ra thàng từng sheet riêng biệt trong 1 file excel.

Thanks and regards,
Quang
Cho mình hỏi là bạn muốn chuyển toàn bộ bảng có trong Database hay là chuyển 1 số nào đó thôi.
Đây là code chuyển toàn bộ Table ra Excel

Mã:
Private Sub cmdToExcel_Click()
On Error Resume Next
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim tdfs As DAO.TableDefs
Set dbs = CurrentDb()
Set tdfs = dbs.TableDefs
For Each tdf In tdfs
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
tdf.Name, "[COLOR=red]C:\DuongDan\[/COLOR]" & tdf.Name & ".xls", True
Next
Set tdfs = Nothing
Set dbs = Nothing
 
End Sub
 
Lần chỉnh sửa cuối:
Cho mình hỏi là bạn muốn chuyển toàn bộ bảng có trong Database hay là chuyển 1 số nào đó thôi.
Đây là code chuyển toàn bộ Table ra Excel

Mã:
Private Sub cmdToExcel_Click()
On Error Resume Next
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim tdfs As DAO.TableDefs
Set dbs = CurrentDb()
Set tdfs = dbs.TableDefs
For Each tdf In tdfs
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
tdf.Name, "[COLOR=red]C:\DuongDan\[/COLOR]" & tdf.Name & ".xls", True
Next
Set tdfs = Nothing
Set dbs = Nothing
 
End Sub

Thanks bạn nhiều lắm,

But trong đọan code này của bạn mình chỉ có thể export tất cả các table thành từng file excel.

Bạn có thể giúp mình export những table theo ý muốn của mình thành từng sheet trong 1 file excel không?

Sorry for your misunderstand.

By any way thanks you alot. :)

Quang
 
Lần chỉnh sửa cuối:
Web KT

Bài viết mới nhất

Back
Top Bottom