Trùng: Nhờ Anh Chị Giúp Đỡ Rút Ngắn Đoạn Code

Liên hệ QC

Gà gà

Thành viên mới
Tham gia
28/12/20
Bài viết
25
Được thích
2
Xin chào anh chị, nhờ các anh chị giúp dùm em rút ngắn đoạn code dưới đây.
Em có 1 file , trong file đó có sheet tên là lệnh cắt.
Trong sheet lệnh cắt có 2 cột, cột C là mã hàng và cột D là số lượng. và ô E5 là ngày
Em muốn copy data của cột C,D và E5 qua một file khác có tên sheet là cutting.
Em có chạy đoạn code dưới đây nhưng nó hơi rườm rà.
Anh chị nào có cách rút gọi vui lòng chỉ giúp em với ạ, em xin cảm ơn.
Dưới đây là đoạn code của em ạ
Sub COPY_dulieu()

Application.ScreenUpdating = False

Dim lastrow As Integer
Dim last As Integer
Dim ls As Integer
Dim lr As Integer
Dim dc As Integer

lastraw = Worksheets("lenh cat").Cells(Rows.Count, 2).End(xlUp).Row
Range("C7:C" & lastraw).Copy
Workbooks.Open Filename:="\\C:\Users\Pro.Hien\desktop\wire inventory checking\production record 2022.xlsx", UpdateLinks:=0

Sheets("cutting").Select
lr = Worksheets("cutting").Cells(Rows.Count, 2).End(xlUp).Row + 1
Range("B" & lr).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Workbooks("production record 2022").Save
Workbooks("production record 2022").Close

last = Worksheets("lenh cat").Cells(Rows.Count, 3).End(xlUp).Row
Range("D7: D" & last).Copy
Workbooks.Open Filename:="\\C:\Users\Pro.Hien\desktop\wire inventory checking\production record 2022.xlsx", UpdateLinks:=0

ls = Worksheets("cutting").Cells(Rows.Count, 4).End(xlUp).Row + 1
Range("D" & lr).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Workbooks("production record 2022").Save
Workbooks("production record 2022").Close

Range("E5").Copy
Workbooks.Open Filename:="\\C:\Users\Pro.Hien\desktop\wire inventory checking\production record 2022.xlsx", UpdateLinks:=0

dc = Worksheets("cutting").Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & dc).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Workbooks("production record 2022").Save
Workbooks("production record 2022").Close
Application.ScreenUpdating = True

End Sub
 
Lần chỉnh sửa cuối:
Lập 2 chủ đề là vi phạm nội qui.
 
Sub Copy
Dim temp1 as variant
Dim temp2 as variant
Dim temp3 as variant
Dim Filename as string

temp1 = Worksheets("lenh cat").Range("C7:C" & Worksheets("lenh cat").Cells(Rows.Count, 2).End(xlUp).Row).value
temp2 = Worksheets("lenh cat").Range("D7:D" & Worksheets("lenh cat").Cells(Rows.Count, 2).End(xlUp).Row).value
temp3 = Worksheets("lenh cat").Range("E5").value
Filename = "\\C:\Users\Pro.Hien\desktop\wire inventory checking\production record 2022.xlsx"
With Workbooks.Open(Filename, 0, , , Sheet4.Range("D10"))
.sheets("cutting").cells(rows.count,2).end(xlup).row.offset(1,0).resize(ubound(temp1,1),ubound(temp1,2)) =temp1
.sheets("cutting").cells(rows.count,4).end(xlup).row.offset(1,0).resize(ubound(temp1,1),ubound(temp1,2)) =temp1
.sheets("cutting").cells(rows.count,1).end(xlup).row.offset(1,0).resize(ubound(temp1,1),ubound(temp1,2)) =temp1
.close true
end sub
 
Sub Copy
Dim temp1 as variant
Dim temp2 as variant
Dim temp3 as variant
Dim Filename as string

temp1 = Worksheets("lenh cat").Range("C7:C" & Worksheets("lenh cat").Cells(Rows.Count, 2).End(xlUp).Row).value
temp2 = Worksheets("lenh cat").Range("D7:D" & Worksheets("lenh cat").Cells(Rows.Count, 2).End(xlUp).Row).value
temp3 = Worksheets("lenh cat").Range("E5").value
Filename = "\\C:\Users\Pro.Hien\desktop\wire inventory checking\production record 2022.xlsx"
With Workbooks.Open(Filename, 0, , , Sheet4.Range("D10"))
.sheets("cutting").cells(rows.count,2).end(xlup).row.offset(1,0).resize(ubound(temp1,1),ubound(temp1,2)) =temp1
.sheets("cutting").cells(rows.count,4).end(xlup).row.offset(1,0).resize(ubound(temp1,1),ubound(temp1,2)) =temp1
.sheets("cutting").cells(rows.count,1).end(xlup).row.offset(1,0).resize(ubound(temp1,1),ubound(temp1,2)) =temp1
.close true
end sub
Bạn ơi
Mình xin gửi file đính kèm
Bạn vui lòng xem xét và giúp mình nha.
 

File đính kèm

  • Practice.xlsm
    9.6 KB · Đọc: 0
  • value.xlsm
    9.9 KB · Đọc: 0
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom