vonguyen3745
Thành viên hoạt động



- Tham gia
- 18/7/09
- Bài viết
- 145
- Được thích
- 5
Chạy subEm có các file nằm ở các thư mục khác nhau, muốn lấy dữ liệu về 1 file tổng hợp. Các file này có sẵn đường dẫn, sheets cần lấy và vùng cần lấy dữ liệu. Thanks
Sub ABC()
Dim sArr(), cn As Object, fRow&
With Sheets("Sheet1")
sArr = .Range("A2", .Range("C" & Rows.Count).End(xlUp)).Value
End With
Set cn = CreateObject("ADODB.Connection")
On Error Resume Next
With Sheets("Sheet2")
For i = 1 To UBound(sArr)
fRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sArr(i, 1) & ";Extended Properties=""Excel 12.0;HDR=No"";"
.Range("A" & fRow).CopyFromRecordset cn.Execute("select * from [" & sArr(i, 2) & "$" & sArr(i, 3) & "] where f1 is not null")
cn.Close
Next i
End With
Set cn = Nothing
End Sub
Bạn thử thế này:Thanks bác HieuCD! Em muốn lấy thêm tên fie để biết dữ liệu đó từ file nào được không bác. Cảm ơn bác rất nhiều ạ.
Sub ABC()
Dim sArr(), cn As Object, fRow&
With Sheets("Sheet1")
sArr = .Range("A2", .Range("C" & Rows.Count).End(xlUp)).Value
End With
Set cn = CreateObject("ADODB.Connection")
On Error Resume Next
With Sheets("Sheet2")
For i = 1 To UBound(sArr)
fRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sArr(i, 1) & ";Extended Properties=""Excel 12.0;HDR=No"";"
.Range("A" & fRow).CopyFromRecordset cn.Execute("select *,'" & sArr(i, 1) & "' from [" & sArr(i, 2) & "$" & sArr(i, 3) & "] where f1 is not null")
cn.Close
Next i
End With
Set cn = Nothing
End Sub
Cái này nếu sheets 2 có dữ liệu nó không ghi đè lên bác nhỉ, khi các file họ cập nhật số liệu, mình muốn lấy dữ liệu mới không được, em muốn sheet 2 có dữ liệu rồi nó ghi đè lên dữ liệu cũ khi mình chạy code được không bác.Bạn thử thế này:
SQL:Sub ABC() Dim sArr(), cn As Object, fRow& With Sheets("Sheet1") sArr = .Range("A2", .Range("C" & Rows.Count).End(xlUp)).Value End With Set cn = CreateObject("ADODB.Connection") On Error Resume Next With Sheets("Sheet2") For i = 1 To UBound(sArr) fRow = .Range("A" & Rows.Count).End(xlUp).Row + 1 cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sArr(i, 1) & ";Extended Properties=""Excel 12.0;HDR=No"";" .Range("A" & fRow).CopyFromRecordset cn.Execute("select *,'" & sArr(i, 1) & "' from [" & sArr(i, 2) & "$" & sArr(i, 3) & "] where f1 is not null") cn.Close Next i End With Set cn = Nothing End Sub
Cái này nếu sheets 2 có dữ liệu nó không ghi đè lên bác nhỉ, khi các file họ cập nhật số liệu, mình muốn lấy dữ liệu mới không được, em muốn sheet 2 có dữ liệu rồi nó ghi đè lên dữ liệu cũ khi mình chạy code được không bác.
Một cách làm khác, bạn tham khảo thêm:Em có các file nằm ở các thư mục khác nhau, muốn lấy dữ liệu về 1 file tổng hợp. Các file này có sẵn đường dẫn, sheets cần lấy và vùng cần lấy dữ liệu. Thanks
Option Explicit
Dim sFile As String
Sub DataLink(ByRef cell_ As Range, ByVal sLink As String, ByVal sFileName As String)
On Error GoTo Err_
cell_.FormulaArray = sLink: cell_.Value = cell_.Value
If Len(sFile) = 0 Then sFile = sFileName Else sFile = sFile & vbNewLine & sFileName
Err_:
End Sub
Sub RunMe()
Dim sheet As Worksheet, start_cell As Range, cell_ As Range, str As String, tmp
Dim sFolder As String, sFileName As String, sShName As String, sRange As String
Dim sLink As String
Application.ScreenUpdating = False
Set sheet = Sheet2: sheet.Cells.ClearContents
Set start_cell = Sheet1.Range("A2")
sFile = Empty
Do Until start_cell.Value = ""
str = start_cell.Value
tmp = Split(str, "\")
sFileName = tmp(UBound(tmp))
sFolder = Mid(str, 1, Len(str) - Len(sFileName) - 1)
If Len(Dir(sFolder & "\" & sFileName)) > 0 Then
sShName = start_cell.Offset(, 1).Value
sRange = start_cell.Offset(, 2).Value
Set cell_ = sheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
Set cell_ = cell_.Resize(sheet.Range(sRange).Rows.Count, sheet.Range(sRange).Columns.Count)
sLink = "='" & sFolder & "\[" & sFileName & "]" & sShName & "'!" & sRange & ""
DataLink cell_, sLink, sFileName
End If
Set start_cell = start_cell.Offset(1)
Loop
Application.ScreenUpdating = True
sheet.Activate
If Len(sFile) > 0 Then
sFile = "File link OK:" & vbNewLine & sFile
End If
MsgBox "Kêt thúc " & vbNewLine & sFile, vbOKOnly + vbInformation
End Sub
Chào Bạn, trong Sub RunMe , Bạn thử sửa dòng:Khi em chạy code của bác
Hoàng Nhật Phương nó tạo ra dãy số 0 xen giữa các dòng dữ liệu; em không biết dãy đó ở đâu ra nhỉ:
View attachment 262685
sLink = "='" & sFolder & "\[" & sFileName & "]" & sShName & "'!" & sRange & ""
sLink = "'" & sFolder & "\[" & sFileName & "]" & sShName & "'!" & sRange & ""
sLink = "=IF(" & sLink & "<>""""," & sLink & ","""")"
Bạn thử lại RunMe sau:View attachment 262694
OK rồi bạn, mình không muốn paste dữ liệu bắt đầu từ ô A2, mình muốn paste bắt đầu từ ô A4 thì chỉnh thế nào bạn nhỉ?
Sub RunMe()
On Error GoTo Loi_roi:
Application.ScreenUpdating = False
Dim sheet As Worksheet, start_cell As Range, cell_ As Range, str As String, tmp, r As Long
Dim sFolder As String, sFileName As String, sShName As String, sRange As String, sLink As String
Const tenfile_batdau As String = "A2"
Const oghidulieu_dautien As String = "A4"
Const shGhiDuLieu As String = "Sheet2"
sFile = Empty
Set sheet = ThisWorkbook.Worksheets(shGhiDuLieu)
sheet.Cells.ClearContents
Set start_cell = Sheet1.Range(tenfile_batdau)
Do Until start_cell.Value = ""
str = start_cell.Value
tmp = Split(str, "\")
sFileName = tmp(UBound(tmp))
sFolder = Mid(str, 1, Len(str) - Len(sFileName) - 1)
If Len(Dir(sFolder & "\" & sFileName)) > 0 Then
sShName = start_cell.Offset(, 1).Value
sRange = start_cell.Offset(, 2).Value
r = sheet.Range("A" & Rows.Count).End(xlUp).Row
If r < sheet.Range(oghidulieu_dautien).Row Then
r = sheet.Range(oghidulieu_dautien).Row
Else
r = r + 1
End If
Set cell_ = sheet.Range("A" & r)
Set cell_ = cell_.Resize(sheet.Range(sRange).Rows.Count, sheet.Range(sRange).Columns.Count)
sLink = "'" & sFolder & "\[" & sFileName & "]" & sShName & "'!" & sRange & ""
sLink = "=IF(" & sLink & "<>""""," & sLink & ","""")"
DataLink cell_, sLink, sFileName
End If
Set start_cell = start_cell.Offset(1)
Loop
Application.ScreenUpdating = True
sheet.Activate
If Len(sFile) > 0 Then
sFile = "File link OK:" & vbNewLine & sFile
Else
sFile = "Không tìm thâ'y file link."
End If
MsgBox "Kêt thúc " & vbNewLine & sFile, vbOKOnly + vbInformation, "Cap nhat OK"
Loi_roi:
If Err.Number <> 0 Then
Application.ScreenUpdating = True
MsgBox "Vui long lien he GPE!", vbCritical + vbOKOnly, "co loi xay ra:"
End If
End Sub
Bạn thiếu đoạn này ở phía trên, code OT gửi lại bài trên chỉ có Sub RunMe thôi:View attachment 262703
Mình chạy nó báo lỗi thế này
Option Explicit
Dim sFile As String
Sub DataLink(ByRef cell_ As Range, ByVal sLink As String, ByVal sFileName As String)
On Error GoTo Err_
cell_.FormulaArray = sLink: cell_.Value = cell_.Value
If Len(sFile) = 0 Then sFile = sFileName Else sFile = sFile & vbNewLine & sFileName
Err_:
End Sub
Option Explicit
Dim sFile As String
Sub DataLink(ByRef cell_ As Range, ByVal sLink As String, ByVal sFileName As String)
On Error GoTo Err_
cell_.FormulaArray = sLink
cell_.Value = cell_.Value
If Len(sFile) = 0 Then sFile = sFileName Else sFile = sFile & vbNewLine & sFileName
Err_:
End Sub
Sub RunMe()
On Error GoTo Loi_roi:
Application.ScreenUpdating = False
Dim sheet As Worksheet, start_cell As Range, cell_ As Range, str As String, tmp, r As Long
Dim sFolder As String, sFileName As String, sShName As String, sRange As String, sLink As String
Const tenFile_batdau As String = "A2"
Const oGhidulieu_dautien As String = "A4"
Const shGhiDuLieu As String = "Sheet2"
sFile = Empty
Set sheet = ThisWorkbook.Worksheets(shGhiDuLieu)
sheet.Cells.ClearContents
Set start_cell = Sheet1.Range(tenFile_batdau)
Do Until start_cell.Value = ""
str = start_cell.Value
tmp = Split(str, "\")
sFileName = tmp(UBound(tmp))
sFolder = Mid(str, 1, Len(str) - Len(sFileName) - 1)
If Len(Dir(sFolder & "\" & sFileName)) > 0 Then
sShName = start_cell.Offset(, 1).Value
sRange = start_cell.Offset(, 2).Value
r = sheet.Range("A" & Rows.Count).End(xlUp).Row
If r < sheet.Range(oGhidulieu_dautien).Row Then
r = sheet.Range(oGhidulieu_dautien).Row
Else
r = r + 1
End If
Set cell_ = sheet.Range("A" & r)
Set cell_ = cell_.Resize(sheet.Range(sRange).Rows.Count, sheet.Range(sRange).Columns.Count)
sLink = "'" & sFolder & "\[" & sFileName & "]" & sShName & "'!" & sRange & ""
sLink = "=IF(" & sLink & "<>""""," & sLink & ","""")"
DataLink cell_, sLink, sFileName
End If
Set start_cell = start_cell.Offset(1)
Loop
Application.ScreenUpdating = True
sheet.Activate
If Len(sFile) > 0 Then
sFile = "File link OK:" & vbNewLine & sFile
Else
sFile = "Không tìm thâ'y file link."
End If
MsgBox "Kêt thúc " & vbNewLine & sFile, vbOKOnly + vbInformation, "Cap nhat OK"
Loi_roi:
If Err.Number <> 0 Then
Application.ScreenUpdating = True
MsgBox "Vui long lien he GPE!", vbCritical + vbOKOnly, "co loi xay ra:"
End If
End Sub
View attachment 262713
View attachment 262714
Mình chạy code trên file có đường dẫn thế này nó báo không tìm thấy link, đường dẫn dài quá có ảnh hưởng gì không bạn
Bạn bỏ dấu chấm trong tên folder xem sao.View attachment 262713
View attachment 262714
Mình chạy code trên file có đường dẫn thế này nó báo không tìm thấy link, đường dẫn dài quá có ảnh hưởng gì không bạn
Không phải dấu "." đâu Anh, OT thử như sau vẫn OK ạ:Bạn bỏ dấu chấm trong tên folder xem sao.
1.TCCB