caonguyen89
Thành viên mới

- Tham gia
- 26/6/18
- Bài viết
- 39
- Được thích
- 4
- Giới tính
- Nam
hiện tại mình đang muốn viết 1 đoạn code để có thể copy dữ liệu từ các file excel khác nhau vào file tổng hợp khi chạy code báo lỗi không tìm thấy file
code như sau :
Sub nhapdulieu()
Dim total As Worksheet, sh As Worksheet
Dim wk As Workbook
Dim strFolderpath As String
Dim selectedFile As Variant
Dim ifileNum As Integer, ilastrowreport As Integer, inumberofrowtopaste As Integer
Dim Rvitri As Range, Rpartcode As Range, RMAKHUON As Range, rsoluong As Range
Dim strfilename As String
Dim icurrentlasrow As Integer, irowstarttopaste As Integer
Set total = ActiveWorkbook.Sheets("total")
strFolderpath = ActiveWorkbook.Path
ChDrive strFoldepath
ChDir strFolderpath
selectedFile = Application.GetOpenFilename( _
File fiter:="Excel File(*.xls),*.xlsx", MultiSelect:=True)
For ifileNum = ibound(selectedFile) To unbound(selectedFile)
strfilename = selectedFile(ifilename)
Set wk = Workbooks.Open(strfilename)
For Each sh In wk.Sheets
If sh.Name Like "*-giao" Then
With sh
ilastrowtotal = .Range("a" & Rows.Count).End(xlUp).Row
inummberofrowtopaste = ilastrowreport - 2 + 1
Set Rvitri = .Range("A2:a" & ilastrowtotal)
Set Rpartcode = .Range("b2:b" & ilastrowtotal)
Set Rpartcode1 = .Range("C2:C" & ilastrowtotal)
Set RMAKHUON = .Range("D2
" & ilastrowtotal)
Set rsoluong = .Range("e2:e" & ilastrowtotal)
With master
icurrentlasrow = .Range("A" & Rows.Count).End(xlUp).Row
irowstarttopaste = icurrentlasrow + 1
.Range("a" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = Rvitri.Value2
.Range("b" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = Rpartcode.Value2
.Range("c" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = Rpartcode1.Value2
.Range("d" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = RMAKHUON.Value2
.Range("e" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = rsoluong.Value2
End With
End With
End If
Next sh
wk.Close
Next
End Sub
mình đính kèm file tổng hợp và 1 file mẫu
mong anh/chị nào rõ chỉ bảo
code như sau :
Sub nhapdulieu()
Dim total As Worksheet, sh As Worksheet
Dim wk As Workbook
Dim strFolderpath As String
Dim selectedFile As Variant
Dim ifileNum As Integer, ilastrowreport As Integer, inumberofrowtopaste As Integer
Dim Rvitri As Range, Rpartcode As Range, RMAKHUON As Range, rsoluong As Range
Dim strfilename As String
Dim icurrentlasrow As Integer, irowstarttopaste As Integer
Set total = ActiveWorkbook.Sheets("total")
strFolderpath = ActiveWorkbook.Path
ChDrive strFoldepath
ChDir strFolderpath
selectedFile = Application.GetOpenFilename( _
File fiter:="Excel File(*.xls),*.xlsx", MultiSelect:=True)
For ifileNum = ibound(selectedFile) To unbound(selectedFile)
strfilename = selectedFile(ifilename)
Set wk = Workbooks.Open(strfilename)
For Each sh In wk.Sheets
If sh.Name Like "*-giao" Then
With sh
ilastrowtotal = .Range("a" & Rows.Count).End(xlUp).Row
inummberofrowtopaste = ilastrowreport - 2 + 1
Set Rvitri = .Range("A2:a" & ilastrowtotal)
Set Rpartcode = .Range("b2:b" & ilastrowtotal)
Set Rpartcode1 = .Range("C2:C" & ilastrowtotal)
Set RMAKHUON = .Range("D2

Set rsoluong = .Range("e2:e" & ilastrowtotal)
With master
icurrentlasrow = .Range("A" & Rows.Count).End(xlUp).Row
irowstarttopaste = icurrentlasrow + 1
.Range("a" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = Rvitri.Value2
.Range("b" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = Rpartcode.Value2
.Range("c" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = Rpartcode1.Value2
.Range("d" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = RMAKHUON.Value2
.Range("e" & irowstarttopaste).Resize(inumberofrowstopaste, 1) = rsoluong.Value2
End With
End With
End If
Next sh
wk.Close
Next
End Sub
mình đính kèm file tổng hợp và 1 file mẫu
mong anh/chị nào rõ chỉ bảo