hungdiep85
Thành viên thường trực




- Tham gia
- 1/6/09
- Bài viết
- 218
- Được thích
- 23
- Giới tính
- Nam
Chào các Thầy
Sửa lỗi giúp em file này với, khi có file đang mở mà chạy code ở file TongHop thì báo lỗi.
Em cảm ơn các Thầy.
Nếu bạn đã am hiểu về code thì dùng ADO cho nó oai, còn nếu chỉ mới tập tành như mình thì dùng code này cho dễ hiểuChào các Thầy
Sửa lỗi giúp em file này với, khi có file đang mở mà chạy code ở file TongHop thì báo lỗi.
Em cảm ơn các Thầy.
Sub Main()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim FileName, SheetName, Path As String, Chk As Boolean
Dim I As Byte, J As Byte, CurWB As Worksheet, WB As Workbook
Path = ThisWorkbook.Path
FileName = Array("File1", "File2", "File3", "File4")
SheetName = Array("Chitiet1", "Chitiet2", "Chitiet3")
Set CurWB = ThisWorkbook.Sheets("TongHop")
CurWB.[A2:J65536].ClearContents
For I = 0 To UBound(FileName)
For Each WB In Workbooks
If WB.Name = FileName(I) Then Chk = True
Next
If Chk = False Then
Workbooks.Open Path & "\" & FileName(I) & "-Chitiet.xlsm"
End If
With Workbooks(FileName(I) & "-Chitiet.xlsm")
For J = 0 To UBound(SheetName)
With .Sheets(FileName(I) & "-" & SheetName(J))
.Range(.[A2], .[J65536].End(3)).Copy
CurWB.[A65536].End(3).Offset(1).PasteSpecial 1
End With
Next
.Close
End With
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "Done"
End Sub
Nếu bạn đã am hiểu về code thì dùng ADO cho nó oai, còn nếu chỉ mới tập tành như mình thì dùng code này cho dễ hiểu
PHP:Sub Main() Application.ScreenUpdating = False Application.DisplayAlerts = False Dim FileName, SheetName, Path As String, Chk As Boolean Dim I As Byte, J As Byte, CurWB As Worksheet, WB As Workbook Path = ThisWorkbook.Path FileName = Array("File1", "File2", "File3", "File4") SheetName = Array("Chitiet1", "Chitiet2", "Chitiet3") Set CurWB = ThisWorkbook.Sheets("TongHop") CurWB.[A2:J65536].ClearContents For I = 0 To UBound(FileName) For Each WB In Workbooks If WB.Name = FileName(I) Then Chk = True Next If Chk = False Then Workbooks.Open Path & "\" & FileName(I) & "-Chitiet.xlsm" End If With Workbooks(FileName(I) & "-Chitiet.xlsm") For J = 0 To UBound(SheetName) With .Sheets(FileName(I) & "-" & SheetName(J)) .Range(.[A2], .[J65536].End(3)).Copy CurWB.[A65536].End(3).Offset(1).PasteSpecial 1 End With Next .Close End With Next Application.ScreenUpdating = True Application.DisplayAlerts = True MsgBox "Done" End Sub