Chào thầy!
Em đang luyện tập copy dữ liệu từ 1 file này sang 1 file khác có điều kiện.
Nhưng khi chạy code thì dữ liệu không được copy qua. Thầy xem sửa giúp em với ạ.
Cảm ơn thầy!
Sub load()
'Mo thu muc chon file
If Sheets("sheet1").Range("L2").Value = "" Then
MsgBox " Ban chua chon ma hang", vbOKOnly
Exit Sub
ElseIf Sheets("sheet1").Range("L2").Value <> "" Then
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Show
'khai bao bien cho workbook
Dim wbth As Workbook
Set wbth = ThisWorkbook
Dim wbtk As Workbook
Set wbtk = Workbooks.Open(.SelectedItems(1))
'dieu kien chon sheet cua workbook duoc chon
If wbth.Sheets("sheet1").Range("L2").Value = "PT" Then
wbtk.Sheets("PT").Activate
ElseIf wbth.Sheets("sheet1").Range("L2").Value = "WI-K" Then
wbtk.Sheets("WI-K").Activate
ElseIf wbth.Sheets("sheet1").Range("I2").Value = "QC-X" Then
wbtk.Sheets("QC-X").Activate
ElseIf wbth.Sheets("sheet1").Range("L2").Value = "QC-W" Then
wbtk.Sheets("QC-W").Activate
ElseIf wbth.Sheets("sheet1").Range("L2").Value = "BR-X" Then
wbtk.Sheets("BR-X").Activate
ElseIf wbth.Sheets("sheet1").Range("L2").Value = "BR-W" Then
wbtk.Sheets("BR-W").Activate
ElseIf wbth.Sheets("sheet1").Range("L2").Value = "ON-G" Then
wbtk.Sheets("ON-G").Activate
ElseIf wbth.Sheets("sheet1").Range("L2").Value = "MB-G" Then
wbtk.Sheets("MB-G").Activate
Else
'khai bao dong dau, dong cuoi, khoang cach
Dim dcth As Long
Dim ddth As Long
Dim kc As Long
dcth = wbth.Sheets("sheet1").Range("B" & Rows.Count).End(xlUp).Row
ddth = 2
kc = dcth - ddth + 1
Dim wstk As Worksheet
Set wstk = wbtk.ActiveSheet
Dim dctk As Long
dctk = wbtk.wstk.Range("H" & Rows.Count).End(xlUp).Row
'nhan du lieu
wbtk.wstk.Range("H" & dctk + 1 & ":K" & dctk + kc).Value = wbth.Sheets("sheet1").Range("B" & ddth & ":E" + dcth).Value
End If
End With
End If
End Sub
Chào bạn bạn có thể đẩy fie lên được không?
Dạ file ở link trên, thầy xem sửa giúp em ạ!
Cảm ơn thầy!!
Bạn xem code mẫu dưới đây nhé:
Sub load()
'Mo thu muc chon file
If Sheets("sheet1").Range("L2").Value = "" Then
MsgBox " Ban chua chon ma hang", vbOKOnly
Exit Sub
ElseIf Sheets("sheet1").Range("L2").Value <> "" Then
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Show
'khai bao bien cho workbook
Dim wbth As Workbook
Set wbth = ThisWorkbook
Dim wbtk As Workbook
Set wbtk = Workbooks.Open(.SelectedItems(1))
'dieu kien chon sheet cua workbook duoc chon
Dim tenSheet As String
tenSheet = wbth.Sheets("sheet1").Range("L2").Value
Dim dcth As Long
Dim ddth As Long
Dim kc As Long
dcth = wbth.Sheets("sheet1").Range("B" & Rows.Count).End(xlUp).Row
ddth = 2
kc = dcth - ddth + 1
Dim dctk As Long
dctk = wbtk.Sheets(tenSheet).Range("H" & Rows.Count).End(xlUp).Row
'nhan du lieu
wbtk.Sheets(tenSheet).Range("H" & dctk + 1 & ":K" & dctk + kc).Value = wbth.Sheets("sheet1").Range("B" & ddth & ":E" & dcth).Value
End With
End If
End Sub
Chú ý:
- Việc xác định tên Sheet dựa theo ô L2 nên bạn chỉ cần gán biến theo tên sheet này là được.
- Dòng code xác định vùng nhận dữ liệu (ở cuối) mình ko rõ bạn sai ở đâu, nhưng khi chạy code báo lỗi. Khi mình viết lại (nội dung giống như cũ) thì chạy được, nên mình chưa rõ nguyên nhân, có thể do sai chính tả.
Chúc bạn học tốt cùng gitiho