Sub Copy_data() 'Lay noi dung tu mot vung xac dinh
Dim shnames, k As Long, lastRow As Long
Dim wb As Workbook
Dim sh As Worksheet
Dim sPath As String, t
' t = Timer
Application.ScreenUpdating = False
sPath = ThisWorkbook.Path & "\" 'Duong dan toi thu muc / hien tai 2 file cung thu muc
shnames = Array(Array("CP", "DA", "GL", "H1", "H2", "HD", "HP", "HY", "MK", "VT", "VY", "HG", "LC", "MC", "TQ", "YB"), _
Array("CP", "DA", "GL", "DTD", "HN2", "HD", "HP", "HY", "MK", "VT", "VY", "HG", "LC", "MC", "TQ", "YB"))
Set sh = ThisWorkbook.Worksheets("TONG HOP")
For k = 0 To 15
Set sh = ThisWorkbook.Worksheets(shnames(1)(k))
On Error Resume Next
lastRow = sh.UsedRange.Find("*", sh.UsedRange(1), xlFormulas, xlPart, xlByRows, xlPrevious).Row
On Error GoTo 0
If lastRow > 0 Then sh.Range("A1:S" & lastRow).ClearContents
Next k
If Dir(sPath & "UNS*.xls*", vbDirectory) = "" Then
MsgBox "Vui lňng copy Master data vŕo cůng folder voi file nŕy"
Exit Sub
End If
If Dir(sPath & "UNS PTHN.xlsb") <> "" Then
Set wb = Workbooks.Open(sPath & "UNS PTHN.xlsb")
For k = 0 To 10
With wb.Worksheets(shnames(0)(k))
lastRow = .Cells(Rows.Count, "S").End(xlUp).Row
.Range("A6:S" & lastRow).AutoFilter Field:=17, Criteria1:="<>0"
.Range("A1:S" & lastRow).Copy ThisWorkbook.Worksheets(shnames(1)(k)).Range("A1")
End With
Next k
wb.Close False
Else
MsgBox "Thieu tap tin UNS PTHN.xlsb"
End If
If Dir(sPath & "UNS NPPP.xlsb") <> "" Then
Set wb = Workbooks.Open(sPath & "UNS NPPP.xlsb")
For k = 11 To 15
With wb.Worksheets(shnames(0)(k))
lastRow = .Cells(Rows.Count, "S").End(xlUp).Row
.Range("A6:S" & lastRow).AutoFilter Field:=17, Criteria1:="<>0"
.Range("A1:S" & lastRow).Copy ThisWorkbook.Worksheets(shnames(1)(k)).Range("A1")
End With
Next k
wb.Close False
Else
MsgBox "Thieu tap tin UNS NPPP.xlsb"
End If
Application.ScreenUpdating = False
MsgBox "Hoŕn Thŕnh", , "Thông Báo"
' Debug.Print Timer - t
End Sub