Nhờ các bạn giúp đỡ tinh giảm code trong vba giúp (2 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

huyhoang_mmyeht

Thành viên hoạt động
Tham gia
5/5/09
Bài viết
142
Được thích
12
Chào các bạn.
mình không dành vba nên lên mạng tham khảo và có viết được đoạn code theo ý mình nhưng mình thấy nó dài dòng quá nên lên đây nhờ các bạn tinh giảm giúp cho nó gọn nhẹ hơn.

yêu cầu của đề bài:
Nhập ID vào đây
- Dòng số 1:tự động lấy số lượng bên sheet KH bỏ vào size tương ứng.
- Dòng số 2 :sumif theo SO và size tương ưng
- Dòng cố 3 : số thiếu còn lại chưa đáp ứng nếu Size nào có TKH=0 thì Hide lại.
-Dòng số 5: từ cột J5:BA5 nếu nhập số lượng mà lớn hơn số lượng còn lại thì không cho nhập.
(Khi nhập ID mới vào tự động đẩy dòng số 8 xuống)

code nằm trong file nhé các bạn.

chân thành cảm ơn.
 

File đính kèm

Chào các bạn.
mình không dành vba nên lên mạng tham khảo và có viết được đoạn code theo ý mình nhưng mình thấy nó dài dòng quá nên lên đây nhờ các bạn tinh giảm giúp cho nó gọn nhẹ hơn.

yêu cầu của đề bài:
Nhập ID vào đây
- Dòng số 1:tự động lấy số lượng bên sheet KH bỏ vào size tương ứng.
- Dòng số 2 :sumif theo SO và size tương ưng
- Dòng cố 3 : số thiếu còn lại chưa đáp ứng nếu Size nào có TKH=0 thì Hide lại.
-Dòng số 5: từ cột J5:BA5 nếu nhập số lượng mà lớn hơn số lượng còn lại thì không cho nhập.
(Khi nhập ID mới vào tự động đẩy dòng số 8 xuống)

code nằm trong file nhé các bạn.

chân thành cảm ơn.
Code sự kiện worksheet_change dài 3 trang? Kinh quá
 
Chào các bạn.
mình không dành vba nên lên mạng tham khảo và có viết được đoạn code theo ý mình nhưng mình thấy nó dài dòng quá nên lên đây nhờ các bạn tinh giảm giúp cho nó gọn nhẹ hơn.

yêu cầu của đề bài:
Nhập ID vào đây
- Dòng số 1:tự động lấy số lượng bên sheet KH bỏ vào size tương ứng.
- Dòng số 2 :sumif theo SO và size tương ưng
- Dòng cố 3 : số thiếu còn lại chưa đáp ứng nếu Size nào có TKH=0 thì Hide lại.
-Dòng số 5: từ cột J5:BA5 nếu nhập số lượng mà lớn hơn số lượng còn lại thì không cho nhập.
(Khi nhập ID mới vào tự động đẩy dòng số 8 xuống)

code nằm trong file nhé các bạn.

chân thành cảm ơn.
Kinh khủng quá bạn ạ! Bạn chỉ cần nói rõ yêu cầu, xong người khác code bạn so sánh lại với cái của mình, chứ dò hết cái này chắc xỉu quá
 
Tôi nhìn code bạn hoàn toàn có thể rút gọn khoảng 1/4 đoạn code đó. Nhưng để hiểu từng lệnh thì mệt quá, thay gì đưa code sao bạn không nêu mục đích muốn đạt được, chứ sửa code ngán quá.
 
1- Yêu cầu đề bài của mình là
KH-Khi gỏ vào ID tự động lấy số lượng kế hoạch điền vào dòng đầu tiên theo hệ size
DLV-Sum số lượng theo SO từng size chi tiết ở bên dưới
TKH-lấy Dlv-KH
ở dòng số 8 lấy thông tin của dòng số 5 Khi gỏ vào ID thì những size nào có số lượng =0 hoặc "" thì ẩn nó đi chỉ để lại size có số lượng <>0
ở dòng số 5 khi gỏ số lượng vào từng size nếu số lượng lớn hơn số còn lại thì không cho
khi gỏ xong chạy về cột ID gỏ ID mới vào thì tự động thêm dòng mới ở và đẩy dòng dữ liệu xuống dòng số 9 và cứ thế

mình có mô tả trong file đó nên nhờ các bạn giúp với.
 
Bạn thử trò này của mình, như sau
Macro sự kiện còn là:
PHP:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xRg As Range, sLt As Range
    Application.ScreenUpdating = False
    If Target.Address = "$H$5" And Len([H5]) >= 5 Then
        Range("A5:F5").Resize.FormulaR1C1 = "=IFERROR(INDEX(KH!R7C[1]:R68686C[1],MATCH(TRIM(R5C8),KH!R7C8:R68686C8,0)),"""")"
        Range("B5:C5").Resize.NumberFormat = "@"
        Range("A5:F5").Value = Range("A5:F5").Value
        Range("J1:BA1").Resize.FormulaR1C1 = "=IFERROR(INDEX(KH!R7C10:R68686C53,MATCH(R5C2,KH!R7C3:R68686C3,0),MATCH(R4C,KH!R4C10:R4C53,0)),0)"
        Range("J1:BA1").Value = Range("J1:BA1").Value
        If (Len([B5]) < 10) Then
            MsgBox "Khong du lieu SO nay"
            Range("H5").Select
        Else
            Range("J2:BA2").ClearContents
            Range("J2:BA2").Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
            Range("J2:BA2").Value = Range("J2:BA2").Value
            If ([I8] = 0) Then
               Rows("8").EntireRow.Delete
            Else
            End If
        Rows("8").EntireRow.Insert
        For Each xRg In Range("$J$3:$BA$3")
            If (Target.Address = "$H$6") Then
                xRg.EntireColumn.Hidden = False
            ElseIf xRg.Value = 0 Then
                xRg.EntireColumn.Hidden = False
                xRg.EntireColumn.Hidden = True
            Else
                xRg.EntireColumn.Hidden = False
            End If
        Next xRg
        Range("J5:BA5").ClearContents
        Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        Range("A8:H8").Value = Range("A8:H8").Value
        End If
    Else
    End If
    If Target.Address = "$J$5" Then
        GPE Target
    ElseIf Target.Address = "$K$5" Then
        GPE Target
    ElseIf Target.Address = "$L$5" Then
        GPE Target
     ElseIf Target.Address = "$M$5" Then
        GPE Target
    ElseIf Target.Address = "$N$5" Then
        GPE Target
    ElseIf Target.Address = "$O$5" Then
        GPE Target
    ElseIf Target.Address = "$P$5" Then
        GPE Target
    ElseIf Target.Address = "$Q$5" Then
        GPE Target
    ElseIf Target.Address = "$R$5" Then
        GPE Target
    ElseIf Target.Address = "$S$5" Then
        GPE Target
    ElseIf Target.Address = "$T$5" Then
        GPE Target
    ElseIf Target.Address = "$U$5" Then
        GPE Target
    ElseIf Target.Address = "$V$5" Then
        GPE Target
    ElseIf Target.Address = "$W$5" Then
        GPE Target
    ElseIf Target.Address = "$X$5" Then
        GPE Target
    ElseIf Target.Address = "$Y$5" Then
        GPE Target
    ElseIf Target.Address = "$Z$5" Then
        GPE Target
    ElseIf Target.Address = "$AA$5" Then
        GPE Target
    ElseIf Target.Address = "$AB$5" Then
        GPE Target
    ElseIf Target.Address = "$AC$5" Then
        GPE Target
    ElseIf Target.Address = "$AD$5" Then
        GPE Target
    ElseIf Target.Address = "$AE$5" Then
        GPE Target
    ElseIf Target.Address = "$AF$5" Then
        GPE Target
    ElseIf Target.Address = "$AG$5" Then
        GPE Target
    ElseIf Target.Address = "$AH$5" Then
        GPE Target
    ElseIf Target.Address = "$AI$5" Then
        GPE Target
    ElseIf Target.Address = "$AJ$5" Then
        GPE Target
    ElseIf Target.Address = "$AK$5" Then
        GPE Target
    ElseIf Target.Address = "$AL$5" Then
        GPE Target
    ElseIf Target.Address = "$AM$5" Then
        GPE Target
    ElseIf Target.Address = "$AN$5" Then
        GPE Target
    ElseIf Target.Address = "$AO$5" Then
        GPE Target
    ElseIf Target.Address = "$AP$5" Then
        GPE Target
    ElseIf Target.Address = "$AQ$5" Then
        GPE Target
    ElseIf Target.Address = "$AR$5" Then
        GPE Target
    ElseIf Target.Address = "$AS$5" Then
        GPE Target
    ElseIf Target.Address = "$AT$5" Then
        GPE Target
    ElseIf Target.Address = "$AU$5" Then
        GPE Target
    ElseIf Target.Address = "$AV$5" Then
        GPE Target
    ElseIf Target.Address = "$AW$5" Then
        GPE Target
    ElseIf Target.Address = "$AX$5" Then
        GPE Target
    ElseIf Target.Address = "$AY$5" Then
        GPE Target
    ElseIf Target.Address = "$AZ$5" Then
        GPE Target
    ElseIf Target.Address = "$BA$5" Then
        GPE Target
    End If
    Application.ScreenUpdating = True
End Sub
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '
Kèm theo với nó là macro con:
Mã:
Sub GPE(Targt As Range)
With Targt.Offset(3)
    .Resize.FormulaR1C1 = "=R5C"
    .Value = .Value
End With
With Targt.Offset(-3)
    .Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
    .Value = .Value
    If .Offset(-1).Value - .Value < 0 Then
        MsgBox "Không Thê Nhâp Sô Lón Hon KH!"
        Targt.Select:                   targrt.Value = Space(0)
    End If
End With
End Sub

Nếu bạn thử nghiệm thấy được ta sẽ tiếp tục rút xuống nữa với fương thức Intersect()
 
Các bạn có thể xem video minh họa vì sợ mình trình bày các bạn chưa hiểu ý mình.
Bài đã được tự động gộp:

Bạn thử trò này của mình, như sau
Macro sự kiện còn là:
PHP:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xRg As Range, sLt As Range
    Application.ScreenUpdating = False
    If Target.Address = "$H$5" And Len([H5]) >= 5 Then
        Range("A5:F5").Resize.FormulaR1C1 = "=IFERROR(INDEX(KH!R7C[1]:R68686C[1],MATCH(TRIM(R5C8),KH!R7C8:R68686C8,0)),"""")"
        Range("B5:C5").Resize.NumberFormat = "@"
        Range("A5:F5").Value = Range("A5:F5").Value
        Range("J1:BA1").Resize.FormulaR1C1 = "=IFERROR(INDEX(KH!R7C10:R68686C53,MATCH(R5C2,KH!R7C3:R68686C3,0),MATCH(R4C,KH!R4C10:R4C53,0)),0)"
        Range("J1:BA1").Value = Range("J1:BA1").Value
        If (Len([B5]) < 10) Then
            MsgBox "Khong du lieu SO nay"
            Range("H5").Select
        Else
            Range("J2:BA2").ClearContents
            Range("J2:BA2").Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
            Range("J2:BA2").Value = Range("J2:BA2").Value
            If ([I8] = 0) Then
               Rows("8").EntireRow.Delete
            Else
            End If
        Rows("8").EntireRow.Insert
        For Each xRg In Range("$J$3:$BA$3")
            If (Target.Address = "$H$6") Then
                xRg.EntireColumn.Hidden = False
            ElseIf xRg.Value = 0 Then
                xRg.EntireColumn.Hidden = False
                xRg.EntireColumn.Hidden = True
            Else
                xRg.EntireColumn.Hidden = False
            End If
        Next xRg
        Range("J5:BA5").ClearContents
        Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        Range("A8:H8").Value = Range("A8:H8").Value
        End If
    Else
    End If
    If Target.Address = "$J$5" Then
        GPE Target
    ElseIf Target.Address = "$K$5" Then
        GPE Target
    ElseIf Target.Address = "$L$5" Then
        GPE Target
     ElseIf Target.Address = "$M$5" Then
        GPE Target
    ElseIf Target.Address = "$N$5" Then
        GPE Target
    ElseIf Target.Address = "$O$5" Then
        GPE Target
    ElseIf Target.Address = "$P$5" Then
        GPE Target
    ElseIf Target.Address = "$Q$5" Then
        GPE Target
    ElseIf Target.Address = "$R$5" Then
        GPE Target
    ElseIf Target.Address = "$S$5" Then
        GPE Target
    ElseIf Target.Address = "$T$5" Then
        GPE Target
    ElseIf Target.Address = "$U$5" Then
        GPE Target
    ElseIf Target.Address = "$V$5" Then
        GPE Target
    ElseIf Target.Address = "$W$5" Then
        GPE Target
    ElseIf Target.Address = "$X$5" Then
        GPE Target
    ElseIf Target.Address = "$Y$5" Then
        GPE Target
    ElseIf Target.Address = "$Z$5" Then
        GPE Target
    ElseIf Target.Address = "$AA$5" Then
        GPE Target
    ElseIf Target.Address = "$AB$5" Then
        GPE Target
    ElseIf Target.Address = "$AC$5" Then
        GPE Target
    ElseIf Target.Address = "$AD$5" Then
        GPE Target
    ElseIf Target.Address = "$AE$5" Then
        GPE Target
    ElseIf Target.Address = "$AF$5" Then
        GPE Target
    ElseIf Target.Address = "$AG$5" Then
        GPE Target
    ElseIf Target.Address = "$AH$5" Then
        GPE Target
    ElseIf Target.Address = "$AI$5" Then
        GPE Target
    ElseIf Target.Address = "$AJ$5" Then
        GPE Target
    ElseIf Target.Address = "$AK$5" Then
        GPE Target
    ElseIf Target.Address = "$AL$5" Then
        GPE Target
    ElseIf Target.Address = "$AM$5" Then
        GPE Target
    ElseIf Target.Address = "$AN$5" Then
        GPE Target
    ElseIf Target.Address = "$AO$5" Then
        GPE Target
    ElseIf Target.Address = "$AP$5" Then
        GPE Target
    ElseIf Target.Address = "$AQ$5" Then
        GPE Target
    ElseIf Target.Address = "$AR$5" Then
        GPE Target
    ElseIf Target.Address = "$AS$5" Then
        GPE Target
    ElseIf Target.Address = "$AT$5" Then
        GPE Target
    ElseIf Target.Address = "$AU$5" Then
        GPE Target
    ElseIf Target.Address = "$AV$5" Then
        GPE Target
    ElseIf Target.Address = "$AW$5" Then
        GPE Target
    ElseIf Target.Address = "$AX$5" Then
        GPE Target
    ElseIf Target.Address = "$AY$5" Then
        GPE Target
    ElseIf Target.Address = "$AZ$5" Then
        GPE Target
    ElseIf Target.Address = "$BA$5" Then
        GPE Target
    End If
    Application.ScreenUpdating = True
End Sub
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '
Kèm theo với nó là macro con:
Mã:
Sub GPE(Targt As Range)
With Targt.Offset(3)
    .Resize.FormulaR1C1 = "=R5C"
    .Value = .Value
End With
With Targt.Offset(-3)
    .Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
    .Value = .Value
    If .Offset(-1).Value - .Value < 0 Then
        MsgBox "Không Thê Nhâp Sô Lón Hon KH!"
        Targt.Select:                   targrt.Value = Space(0)
    End If
End With
End Sub

Nếu bạn thử nghiệm thấy được ta sẽ tiếp tục rút xuống nữa với fương thức Intersect()
Về cơ bản là được nhưng nếu nhập số lượng lớn hơn KH nó báo lỗi ở dòng này.
Targt.Select: targrt.Value = Space(0)
 

File đính kèm

Lần chỉnh sửa cuối:
Các bạn có thể xem video minh họa vì sợ mình trình bày các bạn chưa hiểu ý mình.
Bài đã được tự động gộp:


Về cơ bản là được nhưng nếu nhập số lượng lớn hơn KH nó báo lỗi ở dòng này.
Targt.Select: targrt.Value = Space(0)
Chất lượng video kém quá không xem được, bạn diễn tả cũng chưa hiểu hết ý thôi sửa theo code trong file của bạn vậy.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                .Range("J2:BA2").Value = .Range("J2:BA2").Value
            End If
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        .Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        Target.Offset(-3).Value = Target.Offset(3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Chất lượng video kém quá không xem được, bạn diễn tả cũng chưa hiểu hết ý thôi sửa theo code trong file của bạn vậy.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                .Range("J2:BA2").Value = .Range("J2:BA2").Value
            End If
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        .Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        Target.Offset(-3).Value = Target.Offset(3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Bạn có thể xem video này thì GPE không cho tải file lớn.
https://drive.google.com/file/d/1ByPd5SMjUnx22a3gdysfMEqTQb2WDefP/view?usp=sharing
Bài đã được tự động gộp:

Chất lượng video kém quá không xem được, bạn diễn tả cũng chưa hiểu hết ý thôi sửa theo code trong file của bạn vậy.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                .Range("J2:BA2").Value = .Range("J2:BA2").Value
            End If
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        .Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        Target.Offset(-3).Value = Target.Offset(3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Code của bạn bị thiếu hàm sum trên dòng DLV và dòng lấy dữ liệu ở dòng số 8
 
Bạn có thể xem video này thì GPE không cho tải file lớn.
https://drive.google.com/file/d/1ByPd5SMjUnx22a3gdysfMEqTQb2WDefP/view?usp=sharing
Bài đã được tự động gộp:


Code của bạn bị thiếu hàm sum trên dòng DLV và dòng lấy dữ liệu ở dòng số 8
Sửa lại thế này xem sao.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                '.Range("J2:BA2").Value = .Range("J2:BA2").Value
            End If
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        .Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        'Target.Offset(-3).Value = Target.Offset(3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Lần chỉnh sửa cuối:
Sửa lại thế này xem sao.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").Resize.FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                '.Range("J2:BA2").Value = .Range("J2:BA2").Value
            End If
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        .Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        'Target.Offset(-3).Value = Target.Offset(3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Code của bạn vẩn bị hạn chế nếu có thêm đoạn này
.Range("J2:BA2").Value = .Range("J2:BA2").Value
mục đích của mình là nó vẩn cập nhập được số liệu sau đó value nó lại để không còn công thức giúp bảng tính nhẹ hơn.
 
Code của bạn vẩn bị hạn chế nếu có thêm đoạn này
.Range("J2:BA2").Value = .Range("J2:BA2").Value
mục đích của mình là nó vẩn cập nhập được số liệu sau đó value nó lại để không còn công thức giúp bảng tính nhẹ hơn.
Tôi để đó nếu bạn cần thì dùng còn không cần có thẻ bỏ đi mà.
 
Tôi để đó nếu bạn cần thì dùng còn không cần có thẻ bỏ đi mà.
Ý mình là dòng đó cần nhưng làm sao để nó cập nhập lại khi mình thay đổi bất cứ thông tin nào trong dòng số 5
code của bạn cũng chưa đáp ứng điều kiện mình càn là nếu gỏ ID không có nó sẽ báo lỗi cho người dùng biết.
dòng số 8 nó nó sẽ không thay đổi nếu gỏ ID không đúng hoặc có số total =0
cột DLV là nó luôn luôn sum theo điều kiện SO bên dưới để biết số còn lại bao nhiêu.

vậy bạn có thể giúp sữa lại hộ mình nhé.
 
Ý mình là dòng đó cần nhưng làm sao để nó cập nhập lại khi mình thay đổi bất cứ thông tin nào trong dòng số 5
code của bạn cũng chưa đáp ứng điều kiện mình càn là nếu gỏ ID không có nó sẽ báo lỗi cho người dùng biết.
dòng số 8 nó nó sẽ không thay đổi nếu gỏ ID không đúng hoặc có số total =0
cột DLV là nó luôn luôn sum theo điều kiện SO bên dưới để biết số còn lại bao nhiêu.

vậy bạn có thể giúp sữa lại hộ mình nhé.
Sửa lại lần nửa xem.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                .Range("J2:BA2").Value = .Range("J2:BA2").Value
            Else
                MsgBox "Ma so ban vua nhap khong co"
                Exit Sub
            End If
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        .Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        Target.Offset(-3).Value = Target.Offset(3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Sửa lại lần nửa xem.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                .Range("J2:BA2").Value = .Range("J2:BA2").Value
            Else
                MsgBox "Ma so ban vua nhap khong co"
                Exit Sub
            End If
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").Resize.FormulaR1C1 = "=R5C"
        .Range("I8").Resize.FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        Target.Offset(-3).Value = Target.Offset(3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Cảm ơn bạn đã nhiệt tình nhưng nó bị lỗi nữa rồi
bạn xem video ngắn nhé
 

File đính kèm

Cảm ơn bạn đã nhiệt tình nhưng nó bị lỗi nữa rồi
bạn xem video ngắn nhé
Bạn thử lại code này nhé.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                .Range("J2:BA2").Value = .Range("J2:BA2").Value
            Else
                MsgBox "Ma so ban vua nhap khong co"
                Application.EnableEvents = True
                Application.ScreenUpdating = True
                Exit Sub
            End If
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").FormulaR1C1 = "=R5C"
        .Range("I8").FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        Target.Offset(-3).Value = Target.Offset(-3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Bạn thử lại code này nhé.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, uRng As Range
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    With Sheet1
    If Target.Address = "$H$5" Then
            Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
            If Not Rng Is Nothing Then
                .Range("A5:F5").Value = Rng.Offset(, -6).Resize(, 6).Value
                .Range("J1:BA1").Value = Rng.Offset(, 2).Resize(, 44).Value
                .Range("J2:BA2").FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
                .Range("J2:BA2").Value = .Range("J2:BA2").Value
            Else
                MsgBox "Ma so ban vua nhap khong co"
                Application.EnableEvents = True
                Application.ScreenUpdating = True
                Exit Sub
            End If
        If ([I8] = 0) Then Rows("8").EntireRow.Delete
        Rows("8").EntireRow.Insert
        .Range("J5:BA5").ClearContents
        .Range("A8:H8").FormulaR1C1 = "=R5C"
        .Range("I8").FormulaR1C1 = "=SUM(RC10:RC53)"
        .Range("A8:H8").Value = .Range("A8:H8").Value
        Set Rng = Nothing
        .Range("$J$3:$BA$3").EntireColumn.Hidden = False
        For Each Rng In Range("$J$3:$BA$3")
            If Rng.Value = 0 Then
                If uRng Is Nothing Then
                    Set uRng = Rng
                Else
                    Set uRng = Union(uRng, Rng)
                End If
            End If
        Next Rng
        If Not uRng Is Nothing Then uRng.EntireColumn.Hidden = True
    End If
    If Not Application.Intersect(Range("J5:BA5"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Target.Offset(3).FormulaR1C1 = "=R5C"
        Target.Offset(3).Value = Target.Offset(3).Value
        Target.Offset(-3).FormulaR1C1 = "=SUMIF(R8C2:R68653C2,R5C2,R8C:R68653C)"
        Target.Offset(-3).Value = Target.Offset(-3).Value
        If (Target.Offset(-4).Value - Target.Offset(-3).Value < 0) Then
            MsgBox "Khong the nhap lon hon KH"
                Target.Value = Empty
                Target.Select
        End If
    End If
    End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Cảm ơn bạn thật nhiều bạn có thể giúp mình giải thích đoạn code cho mình hiểu với được không?
 
Cảm ơn bạn thật nhiều bạn có thể giúp mình giải thích đoạn code cho mình hiểu với được không?
Xin lỗi vì tôi không biết giải thích cả mâm code vậy đâu nhé. Mách bạn thế này, cái nào chưa hiểu thử Msgbox nó lên và xem kết quả và từ từ sẽ ngấm, ví dụ để xem lệnh
Mã:
 Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
là gì? thì phía dưới nó dùng lệnh
Mã:
Msgbox Rng.Address
Còn muốn biết lệnh
Mã:
Rng.Offset(, 2).Resize(, 44).Value
là gì thì ở phía dưới nó vẫn Msgbox như sau:
Mã:
Msgbox Rng.Offset(, 2).Resize(, 44).Address
 
Xin lỗi vì tôi không biết giải thích cả mâm code vậy đâu nhé. Mách bạn thế này, cái nào chưa hiểu thử Msgbox nó lên và xem kết quả và từ từ sẽ ngấm, ví dụ để xem lệnh
Mã:
 Set Rng = Sheet2.Range("H:H").Find(Target.Value2, , xlValues, xlWhole, , , True)
là gì? thì phía dưới nó dùng lệnh
Mã:
Msgbox Rng.Address
Còn muốn biết lệnh
Mã:
Rng.Offset(, 2).Resize(, 44).Value
là gì thì ở phía dưới nó vẫn Msgbox như sau:
Mã:
Msgbox Rng.Offset(, 2).Resize(, 44).Address
Bạn thương thì thương cho chót giúp mình, có cách nào khi gỏ nó chỉ chạy qua chạy lại giữa các cell ID và size được tô mầu không bạn, mình dùng locker cell nhưng nó chạy dựt dựt thấy hơi khó chịu.
 

File đính kèm

Bạn thương thì thương cho chót giúp mình, có cách nào khi gỏ nó chỉ chạy qua chạy lại giữa các cell ID và size được tô mầu không bạn, mình dùng locker cell nhưng nó chạy dựt dựt thấy hơi khó chịu.
Bạn protect sheet rồi lấy gì sửa.
 
Web KT

Bài viết mới nhất

Back
Top Bottom