Nhờ thầy, cô, anh, chị giúp code thay hàm Vlookup có điều kiện.

Liên hệ QC

hoangminh2018

Thành viên chính thức
Tham gia
31/10/18
Bài viết
58
Được thích
4
Em nhờ mọi người giúp em code thay hàm vlookup có điều kiện trong file em có ghi rõ.
Bên em có nhiều mặt hàng khi NV bán được thì tính theo tỉ lệ hoa hồng bình thường, nhưng đối với hai mặt hàng A và B thì tính cách khác.
Nếu hoa hồng (tính theo tỉ lệ - bên sheet DMNV) của hàng hóa A hoặc B trong cùng 1 ngày (có phát sinh bán) dưới 1 triệu thì cho 1 triệu - các dòng còn lại trong ngày đó của NV đó = 0, còn nếu hoa hồng >=1 triệu thì tính theo tỉ lệ hoa hồng.
 

File đính kèm

  • hoi code thay vlookup.xlsm
    19.2 KB · Đọc: 11
Cho em hỏi có khó quá không, hay là cách em diễn giải làm mọi người không hiểu ạ.
 
Upvote 0
Em nhờ mọi người giúp em code thay hàm vlookup có điều kiện trong file em có ghi rõ.
Bên em có nhiều mặt hàng khi NV bán được thì tính theo tỉ lệ hoa hồng bình thường, nhưng đối với hai mặt hàng A và B thì tính cách khác.
Nếu hoa hồng (tính theo tỉ lệ - bên sheet DMNV) của hàng hóa A hoặc B trong cùng 1 ngày (có phát sinh bán) dưới 1 triệu thì cho 1 triệu - các dòng còn lại trong ngày đó của NV đó = 0, còn nếu hoa hồng >=1 triệu thì tính theo tỉ lệ hoa hồng.
Vấn đề là lúc đầu dữ liệu của bạn có những gì.Bạn đưa ra 1 cái bảng ai biết làm từ chỗ nào cho bạn.Bạn nên đưa ra bảng dữ liệu ban đầu nữa để còn biết.
 
Upvote 0
Vấn đề là lúc đầu dữ liệu của bạn có những gì.Bạn đưa ra 1 cái bảng ai biết làm từ chỗ nào cho bạn.Bạn nên đưa ra bảng dữ liệu ban đầu nữa để còn biết.
Dạ dữ liệu lúc đầu của em có 2 sheets (DMHangHoa và DM NV)
Còn sheet BangTinh thì em sẽ điền dũ liệu từ cột A đến cột I còn các cột J,K, L là viết code ra kết quả ạ.
Cảm ơn anh đã trả lời.
Bài đã được tự động gộp:

Vấn đề là lúc đầu dữ liệu của bạn có những gì.Bạn đưa ra 1 cái bảng ai biết làm từ chỗ nào cho bạn.Bạn nên đưa ra bảng dữ liệu ban đầu nữa để còn biết.
dữ liệu ban đàu đây ạ.
 

File đính kèm

  • hoi code thay vlookup du lieu ban dau.xlsm
    18.9 KB · Đọc: 9
Lần chỉnh sửa cuối:
Upvote 0
Dạ dữ liệu lúc đầu của em có 2 sheets (DMHangHoa và DM NV)
Còn sheet BangTinh thì em sẽ điền dũ liệu từ cột A đến cột I còn các cột J,K, L là viết code ra kết quả ạ.
Cảm ơn anh đã trả lời.
Bài đã được tự động gộp:


dữ liệu ban đàu đây ạ.
Bạn thử code này nhé.
Mã:
Sub bangtinh()
    Dim arr, data, i As Long, j As Long, k As Long, dic As Object, lr As Long, b As Integer, dk As String, c As Long, d As Double
    Set dic = CreateObject("scripting.dictionary")
        With Sheets("DMHangHoa")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             arr = .Range("A2:E" & lr).Value
             For i = 1 To UBound(arr)
                If i > 2 Then If arr(i, 1) <> Empty Then b = 1
                 If Not dic.exists(arr(i, 2)) Then
                    dic.Add arr(i, 2), Array(i, b)
                 End If
             Next i
        End With
        With Sheets("DMNV")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             data = .Range("A2:E" & lr).Value
             For i = 1 To UBound(data)
                 dk = data(i, 2) & "#"
                 If Not dic.exists(dk) Then
                    dic.Add dk, data(i, 5)
                 End If
             Next i
       End With
       With Sheets("BangTinh")
            lr = .Range("B" & Rows.Count).End(xlUp).Row
            If lr < 7 Then Exit Sub
                .Range("J7:L" & lr).ClearContents
                data = .Range("A7:L" & lr).Value
                For i = 1 To UBound(data)
                    If dic.exists(data(i, 4)) Then
                      c = dic.Item(data(i, 4))(0)
                      If data(i, 9) = 1 Then
                         data(i, 11) = arr(c, 4) * data(i, 8)
                         data(i, 10) = arr(c, 4)
                      Else
                         data(i, 10) = arr(c, 5)
                         data(i, 11) = arr(c, 5) * data(i, 8)
                      End If
                    End If
                 dk = data(i, 1) & data(i, 4)
                    If Not dic.exists(dk) Then
                       dic.Add dk, data(i, 11)
                    Else
                       dic.Item(dk) = dic.Item(dk) + data(i, 11)
                    End If
                Next i
                For i = 1 To UBound(data)
                    c = dic.Item(data(i, 4))(1)
                    dk = data(i, 1) & data(i, 4)
                    d = dic.Item(dk)
                    If c = 1 Then
                       If d < 1000000 Then
                           If d <> -1 Then
                               data(i, 12) = 1000000
                              dic.Item(dk) = -1
                           End If
                       Else
                           data(i, 12) = dic.Item(data(i, 2) & "#") * data(i, 11)
                       End If
                    Else
                           data(i, 12) = dic.Item(data(i, 2) & "#") * data(i, 11)
                    End If
                Next i
        .Range("A7:L" & lr).Value = data
   End With
End Sub
 

File đính kèm

  • hoi code thay vlookup du lieu ban dau.xlsm
    28.7 KB · Đọc: 4
Upvote 0
Bạn thử code này nhé.
Mã:
Sub bangtinh()
    Dim arr, data, i As Long, j As Long, k As Long, dic As Object, lr As Long, b As Integer, dk As String, c As Long, d As Double
    Set dic = CreateObject("scripting.dictionary")
        With Sheets("DMHangHoa")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             arr = .Range("A2:E" & lr).Value
             For i = 1 To UBound(arr)
                If i > 2 Then If arr(i, 1) <> Empty Then b = 1
                 If Not dic.exists(arr(i, 2)) Then
                    dic.Add arr(i, 2), Array(i, b)
                 End If
             Next i
        End With
        With Sheets("DMNV")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             data = .Range("A2:E" & lr).Value
             For i = 1 To UBound(data)
                 dk = data(i, 2) & "#"
                 If Not dic.exists(dk) Then
                    dic.Add dk, data(i, 5)
                 End If
             Next i
       End With
       With Sheets("BangTinh")
            lr = .Range("B" & Rows.Count).End(xlUp).Row
            If lr < 7 Then Exit Sub
                .Range("J7:L" & lr).ClearContents
                data = .Range("A7:L" & lr).Value
                For i = 1 To UBound(data)
                    If dic.exists(data(i, 4)) Then
                      c = dic.Item(data(i, 4))(0)
                      If data(i, 9) = 1 Then
                         data(i, 11) = arr(c, 4) * data(i, 8)
                         data(i, 10) = arr(c, 4)
                      Else
                         data(i, 10) = arr(c, 5)
                         data(i, 11) = arr(c, 5) * data(i, 8)
                      End If
                    End If
                 dk = data(i, 1) & data(i, 4)
                    If Not dic.exists(dk) Then
                       dic.Add dk, data(i, 11)
                    Else
                       dic.Item(dk) = dic.Item(dk) + data(i, 11)
                    End If
                Next i
                For i = 1 To UBound(data)
                    c = dic.Item(data(i, 4))(1)
                    dk = data(i, 1) & data(i, 4)
                    d = dic.Item(dk)
                    If c = 1 Then
                       If d < 1000000 Then
                           If d <> -1 Then
                               data(i, 12) = 1000000
                              dic.Item(dk) = -1
                           End If
                       Else
                           data(i, 12) = dic.Item(data(i, 2) & "#") * data(i, 11)
                       End If
                    Else
                           data(i, 12) = dic.Item(data(i, 2) & "#") * data(i, 11)
                    End If
                Next i
        .Range("A7:L" & lr).Value = data
   End With
End Sub
Dạ cảm ơn anh, nhưng em đã test thử, kết quả chưa đúng ạ
Em gởi anh file đã có kết quả.
 

File đính kèm

  • hoi code thay vlookup.xlsm
    28.3 KB · Đọc: 10
Upvote 0
Dạ cảm ơn anh, nhưng em đã test thử, kết quả chưa đúng ạ
Em gởi anh file đã có kết quả.
Bạn xem lại nhé.
Mã:
Sub bangtinh()
    Dim arr, data, i As Long, j As Long, k As Long, dic As Object, lr As Long, b As Integer, dk As String, c As Long, d As Double
    Set dic = CreateObject("scripting.dictionary")
        With Sheets("DMHangHoa")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             arr = .Range("A2:E" & lr).Value
             For i = 1 To UBound(arr)
                If i > 2 Then If arr(i, 1) <> Empty Then b = 1
                 If Not dic.exists(arr(i, 2)) Then
                    dic.Add arr(i, 2), Array(i, b)
                 End If
             Next i
        End With
        With Sheets("DMNV")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             data = .Range("A2:E" & lr).Value
             For i = 1 To UBound(data)
                 dk = data(i, 2) & "#"
                 If Not dic.exists(dk) Then
                    dic.Add dk, data(i, 5)
                 End If
             Next i
       End With
       With Sheets("BangTinh")
            lr = .Range("B" & Rows.Count).End(xlUp).Row
            If lr < 7 Then Exit Sub
                .Range("J7:L" & lr).ClearContents
                data = .Range("A7:L" & lr).Value
                For i = 1 To UBound(data)
                    If dic.exists(data(i, 4)) Then
                      c = dic.Item(data(i, 4))(0)
                      If data(i, 9) = 1 Then
                         data(i, 11) = arr(c, 4) * data(i, 8)
                         data(i, 10) = arr(c, 4)
                      Else
                         data(i, 10) = arr(c, 5)
                         data(i, 11) = arr(c, 5) * data(i, 8)
                      End If
                    End If
                    data(i, 12) = dic.Item(data(i, 2) & "#") * data(i, 11)
                    dk = data(i, 1) & data(i, 4)
                    If Not dic.exists(dk) Then
                       dic.Add dk, data(i, 12)
                    Else
                       dic.Item(dk) = dic.Item(dk) + data(i, 12)
                    End If
                Next i
                For i = 1 To UBound(data)
                    c = dic.Item(data(i, 4))(1)
                    dk = data(i, 1) & data(i, 4)
                    d = dic.Item(dk)
                    If c = 1 Then
                    If d And d < 1000000 Then
                       data(i, 12) = 1000000
                       dic.Item(dk) = 0
                    ElseIf d = 0 Then
                       data(i, 12) = 0
                    End If
                    End If
                Next i
        .Range("A7:L" & lr).Value = data
   End With
End Sub
 
Upvote 0
Bạn xem lại nhé.
Mã:
Sub bangtinh()
    Dim arr, data, i As Long, j As Long, k As Long, dic As Object, lr As Long, b As Integer, dk As String, c As Long, d As Double
    Set dic = CreateObject("scripting.dictionary")
        With Sheets("DMHangHoa")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             arr = .Range("A2:E" & lr).Value
             For i = 1 To UBound(arr)
                If i > 2 Then If arr(i, 1) <> Empty Then b = 1
                 If Not dic.exists(arr(i, 2)) Then
                    dic.Add arr(i, 2), Array(i, b)
                 End If
             Next i
        End With
        With Sheets("DMNV")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             data = .Range("A2:E" & lr).Value
             For i = 1 To UBound(data)
                 dk = data(i, 2) & "#"
                 If Not dic.exists(dk) Then
                    dic.Add dk, data(i, 5)
                 End If
             Next i
       End With
       With Sheets("BangTinh")
            lr = .Range("B" & Rows.Count).End(xlUp).Row
            If lr < 7 Then Exit Sub
                .Range("J7:L" & lr).ClearContents
                data = .Range("A7:L" & lr).Value
                For i = 1 To UBound(data)
                    If dic.exists(data(i, 4)) Then
                      c = dic.Item(data(i, 4))(0)
                      If data(i, 9) = 1 Then
                         data(i, 11) = arr(c, 4) * data(i, 8)
                         data(i, 10) = arr(c, 4)
                      Else
                         data(i, 10) = arr(c, 5)
                         data(i, 11) = arr(c, 5) * data(i, 8)
                      End If
                    End If
                    data(i, 12) = dic.Item(data(i, 2) & "#") * data(i, 11)
                    dk = data(i, 1) & data(i, 4)
                    If Not dic.exists(dk) Then
                       dic.Add dk, data(i, 12)
                    Else
                       dic.Item(dk) = dic.Item(dk) + data(i, 12)
                    End If
                Next i
                For i = 1 To UBound(data)
                    c = dic.Item(data(i, 4))(1)
                    dk = data(i, 1) & data(i, 4)
                    d = dic.Item(dk)
                    If c = 1 Then
                    If d And d < 1000000 Then
                       data(i, 12) = 1000000
                       dic.Item(dk) = 0
                    ElseIf d = 0 Then
                       data(i, 12) = 0
                    End If
                    End If
                Next i
        .Range("A7:L" & lr).Value = data
   End With
End Sub
Dạ kết quả ra đúng như ý ạ, em cảm ơn anh đã giúp đỡ.
 
Upvote 0
Bạn xem lại nhé.
Mã:
Sub bangtinh()
    Dim arr, data, i As Long, j As Long, k As Long, dic As Object, lr As Long, b As Integer, dk As String, c As Long, d As Double
    Set dic = CreateObject("scripting.dictionary")
        With Sheets("DMHangHoa")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             arr = .Range("A2:E" & lr).Value
             For i = 1 To UBound(arr)
                If i > 2 Then If arr(i, 1) <> Empty Then b = 1
                 If Not dic.exists(arr(i, 2)) Then
                    dic.Add arr(i, 2), Array(i, b)
                 End If
             Next i
        End With
        With Sheets("DMNV")
             lr = .Range("B" & Rows.Count).End(xlUp).Row
             data = .Range("A2:E" & lr).Value
             For i = 1 To UBound(data)
                 dk = data(i, 2) & "#"
                 If Not dic.exists(dk) Then
                    dic.Add dk, data(i, 5)
                 End If
             Next i
       End With
       With Sheets("BangTinh")
            lr = .Range("B" & Rows.Count).End(xlUp).Row
            If lr < 7 Then Exit Sub
                .Range("J7:L" & lr).ClearContents
                data = .Range("A7:L" & lr).Value
                For i = 1 To UBound(data)
                    If dic.exists(data(i, 4)) Then
                      c = dic.Item(data(i, 4))(0)
                      If data(i, 9) = 1 Then
                         data(i, 11) = arr(c, 4) * data(i, 8)
                         data(i, 10) = arr(c, 4)
                      Else
                         data(i, 10) = arr(c, 5)
                         data(i, 11) = arr(c, 5) * data(i, 8)
                      End If
                    End If
                    data(i, 12) = dic.Item(data(i, 2) & "#") * data(i, 11)
                    dk = data(i, 1) & data(i, 4)
                    If Not dic.exists(dk) Then
                       dic.Add dk, data(i, 12)
                    Else
                       dic.Item(dk) = dic.Item(dk) + data(i, 12)
                    End If
                Next i
                For i = 1 To UBound(data)
                    c = dic.Item(data(i, 4))(1)
                    dk = data(i, 1) & data(i, 4)
                    d = dic.Item(dk)
                    If c = 1 Then
                    If d And d < 1000000 Then
                       data(i, 12) = 1000000
                       dic.Item(dk) = 0
                    ElseIf d = 0 Then
                       data(i, 12) = 0
                    End If
                    End If
                Next i
        .Range("A7:L" & lr).Value = data
   End With
End Sub
Anh ơi bây giờ em chèn vào thêm trong sheet BangTinh 1 cột (J) tô màu hồng phân biệt tỉ lệ hoa hồng, trong sheet DMNV em thêm cột F là tỉ lệ hoa hồng loại 2. Các điêu kiện vẫn không đổi chỉ là nếu cột J trên sheet BangTinh mà tỉ lệ là 2 (số 2) thì tính theo tỉ lệ hoa hồng loại 2.
 

File đính kèm

  • hoi code thay vlookup.xlsm
    28.3 KB · Đọc: 5
Upvote 0
Web KT
Back
Top Bottom