Bạn thử.Gửi anh chị GPE
Em muốn lấy dữ liệu theo 2 hay nhiều mã bất kỳ, từ dữ liệu nguồn, sau đó nhân với số lượng để được kết quả, như file đính kèm. Nhờ anh chị GPE giúp đỡ với ạ.
Sub gop()
Dim arr, dic As Object, i As Long, lr As Long, kq, a As Long
Set dic = CreateObject("scripting.dictionary")
With Sheet1
lr = .Range("H" & Rows.Count).End(xlUp).Row
arr = .Range("h9:I" & lr).Value
For i = 1 To UBound(arr)
dic.Item(arr(i, 1)) = arr(i, 2)
Next i
lr = .Range("B" & Rows.Count).End(xlUp).Row
arr = .Range("b6:D" & lr).Value
ReDim kq(1 To UBound(arr), 1 To 3)
For i = 1 To UBound(arr)
If dic.exists(arr(i, 1)) Then
a = a + 1
kq(a, 1) = arr(i, 1)
kq(a, 2) = arr(i, 2)
kq(a, 3) = arr(i, 3) * dic.Item(arr(i, 1))
End If
Next i
.Range("L8:N100").ClearContents
If a Then .Range("L8:N8").Resize(a).Value = kq
End With
End Sub
Sub LocDL()
Dim i As Long, j As Long, lrDL As Long, LrLoc As Long, k As Long
Dim arrSource(), arrMa(), arrResult()
With Sheet1
lrDL = .Range("B" & Rows.Count).End(xlUp).Row
LrLoc = .Range("H" & Rows.Count).End(xlUp).Row
arrSource = .Range("B6:D" & lrDL).Value
arrMa = .Range("H9:I" & LrLoc).Value
ReDim arrResult(1 To UBound(arrSource, 1), 1 To 3)
For i = 1 To UBound(arrMa, 1)
For j = 1 To UBound(arrSource, 1)
If arrSource(j, 1) = arrMa(i, 1) Then
k = k + 1
arrResult(k, 1) = arrSource(j, 1)
arrResult(k, 2) = arrSource(j, 2)
arrResult(k, 3) = arrSource(j, 3) * arrMa(i, 2)
End If
Next j
Next i
End With
Sheet3.Range("A2").Resize(UBound(arrResult, 1), 3) = arrResult
End Sub