Chào các bác!
Em đang tập viết code mà nhiều chỗ chưa hiểu lắm
Em có đoạn code này nó báo lỗi "For each control variable must be variant or object" ở chỗ key
mà em chưa biết sửa thế nào, các bác chỉ giúp em với ạ
(em muốn tổng hợp lại dữ liệu, nếu cùng FG và RM thì số lượng bằng tổng số lượng ạ)
Sub Consol()
Dim ws As Worksheet, lr As Long, Dic As Object, i&, key As String
Dim FG As String, RM As String, Qty As Double
Set ws = ThisWorkbook.Sheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")
lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
FG = ws.Cells(i, 1).Value
RM = ws.Cells(i, 2).Value
Qty = ws.Cells(i, 3).Value
key = FG & "|" & RM
If Dic.exists(key) Then
Dic(key) = Dic(key) + Qty
Else
Dic.Add key, Qty
End If
Next i
ws.Range("E1").Value = "Ma san pham"
ws.Range("F1").Value = "Ma lieu"
ws.Range("G1").Value = "Qty"
i = 2
For Each key In Dic.Keys
ws.Cells(i, 5).Value = Split(key, "|")(0)
ws.Cells(i, 6).Value = Split(key, "|")(1)
ws.Cells(i, 7).Value = Dic(key)
i = i + 1
Next key
MsgBox "Done!"
End Sub
Em đang tập viết code mà nhiều chỗ chưa hiểu lắm
Em có đoạn code này nó báo lỗi "For each control variable must be variant or object" ở chỗ key
mà em chưa biết sửa thế nào, các bác chỉ giúp em với ạ
(em muốn tổng hợp lại dữ liệu, nếu cùng FG và RM thì số lượng bằng tổng số lượng ạ)
Sub Consol()
Dim ws As Worksheet, lr As Long, Dic As Object, i&, key As String
Dim FG As String, RM As String, Qty As Double
Set ws = ThisWorkbook.Sheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")
lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
FG = ws.Cells(i, 1).Value
RM = ws.Cells(i, 2).Value
Qty = ws.Cells(i, 3).Value
key = FG & "|" & RM
If Dic.exists(key) Then
Dic(key) = Dic(key) + Qty
Else
Dic.Add key, Qty
End If
Next i
ws.Range("E1").Value = "Ma san pham"
ws.Range("F1").Value = "Ma lieu"
ws.Range("G1").Value = "Qty"
i = 2
For Each key In Dic.Keys
ws.Cells(i, 5).Value = Split(key, "|")(0)
ws.Cells(i, 6).Value = Split(key, "|")(1)
ws.Cells(i, 7).Value = Dic(key)
i = i + 1
Next key
MsgBox "Done!"
End Sub