Sub GetData1()
Dim FName As String, FPath As String, eCol As Integer, i, j, Tb(), OldVal(1 To 2) As Boolean
Dim Wb As Workbook, Sh As Worksheet
On Error Resume Next
[COLOR=#008000][I]'Tao mang danh sach cac thoi diem TH tuong ung cac cot tren file chi tiet[/I][/COLOR]
Tb = Array("1: T5/2015", "2: Q3/2015", "3: Q4/2015", "4: Q1/2016", _
"5: Q2/2016", "6: Q3/2016", "7: Q4/2016", "8: Q1/2017", "9: T5/2017")
[I][COLOR=#008000]'Thong bao thu muc mac dinh va thay the neu can[/COLOR][/I]
FPath = InputBox("Folder chua cac file CT duoi day la mac dinh." & Chr(13) & _
"Neu doi Folder khac thi ban thay the vao.", "THONG BAO", ThisWorkbook.Path)
[I][COLOR=#008000]'Bat buoc phai nhap 1 ky nao do[/COLOR][/I]
Do While InStr(1, "1,2,3,4,5,6,7,8,9", eCol) = 0
eCol = InputBox("Nhap so tuong ung tung thoi ky" & Chr(13) & Join(Tb, Chr(13)), "THONG BAO", 1)
Loop
[I][COLOR=#008000]'Luu lai cac thiet lap moi truong cua may de sau khi su ly xong ta tra ve nguyen trang
'Thiet lap moi truong de code chay hieu qua[/COLOR][/I]
OldVal(1) = Application.ScreenUpdating: If OldVal(1) Then Application.ScreenUpdating = False
OldVal(2) = Application.DisplayAlerts: If OldVal(2) Then Application.DisplayAlerts = False
[I][COLOR=#008000]'Xoa BTH[/COLOR][/I]
Sheet1.[C8:Q42].ClearContents
[I][COLOR=#008000]'Giai doan chep[/COLOR][/I]
Do
[I][COLOR=#008000]'Bat dau thi i=8 va sau do moi vong lap tang them 1 dong den het data[/COLOR][/I]
i = IIf(i = 0, 8, i + 1)
[I][COLOR=#008000]'Can cu dong i cot C ta xac dinh duoc ten file can lay du lieu (FullName)[/COLOR][/I]
FName = FPath & "\PL01-" & Sheet1.Cells(i, "B") & ".xls"
[I][COLOR=#008000]'Kiem tra file xac dinh co ton tai khong, neu ton tai thi lam cac buoc sau[/COLOR][/I]
If Dir(FName) <> "" Then
[I][COLOR=#008000]'Mo file do ra[/COLOR][/I]
Set Wb = Workbooks.Open(FName)
'[I][COLOR=#008000]Tim Sheet co ten "Mau 01" va chep du lieu[/COLOR][/I]
Set Sh = Wb.Worksheets("Mau 01")
Sheet1.Cells(i, 3).Resize(, 4) = WorksheetFunction.Transpose(Sh.Cells(8, eCol + 2).Resize(4))
Sheet1.Cells(i, 9).Resize(, 9) = WorksheetFunction.Transpose(Sh.Cells(12, eCol + 2).Resize(9))
Set Sh = Nothing
[I][COLOR=#008000]'Dong tra lai file[/COLOR][/I]
Wb.Close
Set Wb = Nothing
End If
[I][COLOR=#008000]'Tuan tu den het danh sach cot C[/COLOR][/I]
Loop Until Sheet1.Cells(i, "B") = ""
[I][COLOR=#008000]'Tra lai nguyen trang cac thiet lap cho may[/COLOR][/I]
Application.ScreenUpdating = OldVal(1)
Application.DisplayAlerts = OldVal(2)
End Sub