Xuân Thanh > 10-06-12, 10:19 PM
Function ExAcEx(tblTabName As String, strFile As String, shSheet As String, Cll As String)
Dim Ex As Excel.Application
Dim fileEx As Workbook
Set fileEx = Ex.Workbooks.Open(strFile)
Dim Ws As Worksheet
Set Ws = fileEx.Worksheets(shSheet)
Dim i, j, k, n As Integer
i = Ws.Range(Cll).Row
n = Ws.Range(Cll).Column
Dim Rs As Recordset
Set Rs = CurrentDb.OpenRecordset(tblTabName, dbOpenTable)
j = Rs.Fields.Count
If Rs.RecordCount > 0 Then
Rs.MoveFirst
Do Until Rs.EOF
For k = 0 To j - 1
Ws.Cells(i, n + k) = Rs.Fields(k)
Next
i = i + 1
Loop
End If
Set Ex = Nothing: Rs.Close
End Function
Xuân Thanh > 11-06-12, 09:47 AM
Function ExAcEx(tblTabName As String, strFile As String, shSheet As String, Cll As String)
Dim Ex As New Excel.Application
Dim fileEx As Workbook
Set fileEx = Ex.Workbooks.Open(strFile)
Dim Ws As Worksheet
Set Ws = fileEx.Worksheets(shSheet)
Dim i, j, k, n As Long
i = Ws.Range(Cll).Row
n = Ws.Range(Cll).Column
Dim Rs As Recordset
Set Rs = CurrentDb.OpenRecordset(tblTabName, dbOpenTable)
j = Rs.Fields.Count
If Rs.RecordCount > 0 Then
Rs.MoveFirst
Do Until Rs.EOF
For k = 0 To j - 1
Ws.Cells(i, n + k) = Rs.Fields(k)
Next
Rs.MoveNext
i = i + 1
Loop
End If
fileEx.Save: fileEx.Close: Set Ex = Nothing: Rs.Close
End Function
domfootwear > 07-08-12, 08:52 AM
(11-06-12, 09:47 AM)Xuân Thanh Đã viết: Sau khi test thử, sửa lại hàm ExAcEx như sau :
1/ Thêm New trước Excel.Application(mở Ex trước khi mở file)
2/ Sau khi Export xong, lưu file Excel và đóng nó bằng dòng lệnh
fileEx.Save
fileEx.Close
Toàn bộ hàm được sửa lại như sau :
Mã PHP:Function ExAcEx(tblTabName As String, strFile As String, shSheet As String, Cll As String)
Dim Ex As New Excel.Application
Dim fileEx As Workbook
Set fileEx = Ex.Workbooks.Open(strFile)
Dim Ws As Worksheet
Set Ws = fileEx.Worksheets(shSheet)
Dim i, j, k, n As Long
i = Ws.Range(Cll).Row
n = Ws.Range(Cll).Column
Dim Rs As Recordset
Set Rs = CurrentDb.OpenRecordset(tblTabName, dbOpenTable)
j = Rs.Fields.Count
If Rs.RecordCount > 0 Then
Rs.MoveFirst
Do Until Rs.EOF
For k = 0 To j - 1
Ws.Cells(i, n + k) = Rs.Fields(k)
Next
Rs.MoveNext
i = i + 1
Loop
End If
fileEx.Save: fileEx.Close: Set Ex = Nothing: Rs.Close
End Function
Test thành công
Xuân Thanh > 08-08-12, 10:50 AM
(07-08-12, 08:52 AM)domfootwear Đã viết: Duyệt qua từng cell dữ liệu sẽ rất mất thời gian anh à, ta có thể dùng mãng để gán nó vào thì sẽ cải thiện tốc độ hơn anh à.
Function ExAcEx(tblTabName As String, strFile As String, shSheet As String, Cll As String)
Dim Ex As New Excel.Application
Dim fileEx As Workbook
Set fileEx = Ex.Workbooks.Open(strFile)
Dim Ws As Worksheet
Set Ws = fileEx.Worksheets(shSheet)
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(tblTabName, dbOpenTable)
Ws.Range(Cll).CopyFromRecordset rs
fileEx.Save: fileEx.Close: Set Ex = Nothing: rs.Close
End Function
taoladarkpro > 30-08-12, 12:01 PM
gioi01 > 11-07-13, 02:29 PM
bangnguyencong > 19-09-13, 11:42 AM
Xuân Thanh > 19-09-13, 02:47 PM
(19-09-13, 11:42 AM)bangnguyencong Đã viết: Bị lỗi dòng màu đỏ dưới đây:
Mã PHP:Function ExAcEx(tblTabName As String, strFile As String, shSheet As String, Cll As String)
Dim Ex As New Excel.Application
Dim fileEx As Workbook
Set fileEx = Ex.Workbooks.Open(strFile)
Dim Ws As Worksheet
Set Ws = fileEx.Worksheets(shSheet)
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(tblTabName, dbOpenTable)
Ws.Range(Cll).CopyFromRecordset rs
fileEx.Save: fileEx.Close: Set Ex = Nothing: rs.Close
End Function
Bạn Xuân Thanh có thể gửi bản demo cho mọi người tham khảo được không?
Trân trọng!
ndthanh29 > 12-06-14, 10:46 PM
Xuân Thanh > 13-06-14, 03:21 PM
Private Sub cmdXuatEx_Click()
Call ExAcEx ("tblDanhsachkhachhang","D:\Excel\Danh sach khach hang.xls","Danh sach","A2")
End Sub