hoabattu3387
Thành viên chính thức


- Tham gia
- 11/9/08
- Bài viết
- 91
- Được thích
- 2
Mình có viết đoạn code copy dữ liệu từ file text vào excel (sử dụng ado). Tuy nhiên báo lỗi "unspecified error". Nhờ các anh chị diễn đàn kiểm tra giúp mình xem đoạn code sau bị lỗi ở đâu và sửa giúp mình với. Mình cảm ơn cả nhà!
Private Sub CommandButton2_Click()
Dim I As Integer
Dim cn As New ADODB.Connection
Dim RCS As New ADODB.Recordset
Dim flog As FileDialog
Set flog = Application.FileDialog(msoFileDialogFilePicker)
With flog
.AllowMultiSelect = True
.Show
.Filters.Clear
.Filters.Add "textfiles", "*.txt"
End With
For I = 1 To flog.SelectedItems.Count
filename = flog.SelectedItems(I)
cn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Left(filename, Len(filename) - Len(Split(filename, "\")(5)) - 1) & ";Extended Properties=""text;HDR=no;FMT=fixed;"";")
With ThisWorkbook.Sheets("ALLDOCS")
a = .[a1048576].End(xlUp).Row
End With
With Sheet1
Sql = "select * from " & Split(filename, "\")(5)
.Range("A" & a + 1).CopyFromRecordset cn.Execute(Sql)
End With
Set cn = Nothing
Next
MsgBox ("Completely")
End Sub