minh dùng vba
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set wskt = Application.ThisWorkbook.Worksheets("KiemTraBanHang")
Dim r As Range
With wskt
If Target.Column = 4 Then
curRow = Target.row
Set r = wskt.Range(Cells(6, 3), Cells(curRow - 1, 3))
lastRow = .Cells(.Rows.Count, 1).End(xlUp).row 'tableKT.ListRows.Count + 5
khhang = wskt.Cells(curRow, 3).Value
If (khhang <> "Tông Tuyê'n" And khhang <> "Tông Ngày" And UCase(Trim(khhang)) <> "CHUABIET") Then
Cells(curRow, 3).Value = Application.WorksheetFunction.Proper(Cells(curRow, 3).Value)
preRow = LastItemLookup(khhang, r) + 5
If (preRow <> 5) Then
'Cells(preRow, 3).Select
Application.ScreenUpdating = False
'Application.EnableEvents = False
Cells(curRow, 6).Value = "=F" & preRow 'Cells(preRow, 6).Value
Cells(curRow, 10).Value = "=J" & preRow 'Cells(preRow, 10).Value
Cells(curRow, 14).Value = "=N" & preRow 'Cells(preRow, 14).Value
Cells(curRow, 18).Value = "=R" & preRow 'Cells(preRow, 18).Value
Cells(curRow, 22).Value = "=W" & preRow 'Cells(preRow, 22).Value
Cells(curRow, 26).Value = "=Z" & preRow 'Cells(preRow, 26).Value
Cells(curRow, 44).Value = "=AS" & preRow 'Cells(preRow, 45).Value
Cells(curRow, 40).Value = "=AO" & preRow 'Cells(preRow, 41).Value
Cells(curRow, 51).Value = "=BB" & preRow 'Cells(preRow, 54).Value
Application.ScreenUpdating = True
'Application.EnableEvents = True
End If
End If
End If 'If Target.Column = 4
End With
Application.OnKey "{F1}", "movetoSP"
Application.OnKey "{F2}", "movetoPV"
Application.OnKey "{F3}", "movetoV"
Application.OnKey "{F5}", "movetoHH"
Application.OnKey "{F6}", "movetoTT"
Application.OnKey "{F4}", "movetoB"
Application.OnKey "{F7}", "themKHcu"
Application.OnKey "{F11}", "tradungtien"
Application.OnKey "{F12}", "customersalecheck"
Application.OnKey "{F9}", "bangbanhang"
End Sub
mình thử dùng
Application.screenupdate = false,
Application.enableEvents = false
Application.Calculation = xlCalculationmanual
để bật tắt update .... nhưng vẫn chậm
Chào bạn đây là bạn sử lý trên range bạn hãy tạo ra module khi thay đổi sự kiện nào đó thì call cái câu lệnh cần gọi và thử lại xem
Việc code chậm do nhiều nguyên nhân. Như bạn mô tả là có sử dụng các đoạn code hỗ trợ tăng tốc độ xử lý nhưng vẫn chậm => có thể xem lại logic của code, xem quy trình như vậy có thể cải thiện được không (nhiều khả năng do quy trình này chưa tối ưu, hoạt động chậm).
Đoạn code này lại gắn trong sự kiện SelectionChange nên bất kỳ thay đổi nào trong ô cũng kích hoạt code => khiến code chậm (do phải làm nhiều việc mà diễn ra liên tục)
=> cách tốt nhất là nên rà soát lại quy trình, thay đổi quy trình để phù hợp hơn với mục tiêu.
Mục tiêu của bạn là rà soát các ô trong cột D để dựa vào đó gán dữ liệu (nếu thỏa mãn điều kiện)
=> có thể bổ sung thêm các hướng:
1. Giới hạn phạm vi diễn ra sự kiện: chỉ trong cột D, tức là bỏ đoạn code If Targe.Column = 4 mà đổi sang đoạn code
IF not Intersect(Target, Range(...)) is nothing then
...
End if
2. Việc rà soát bạn thực hiện theo từng ô trong 1 vùng => có thể cân nhắc sử dụng vòng lặp để chạy code 1 lần thôi, không chạy nhiều lần
3. đánh giá lại xem sự kiện Worksheet_SelectionChange đã phù hợp chưa. Sự kiện SelectionChange diễn ra rất thường xuyên và dễ thao tác nhầm => nếu gắn sự kiện này code nên thật đơn giản và ít tác động