langkhachquaduong
Thành viên chính thức
- Tham gia
- 23/7/19
- Bài viết
- 50
- Được thích
- 8
1. Đây đoạn code mình muốn hỏi: kq(dic.item(arr(i, 2)), dic.item(arr(i, 1))) = kq(dic.item(arr(i, 2)), dic.item(arr(i, 1))) + 1
2.Code tổng quát như sau:
Sub test()
Dim dic As Object, i As Long, arr As Variant, kq As Variant, j As Long, j1 As Long
Set dic = CreateObject("scripting.dictionary")
arr = Range("a1:b6").Value2
ReDim kq(1 To UBound(arr), 1 To 5)
j = 1
j1 = 1
For i = 1 To UBound(arr)
If Not dic.exists(arr(i, 1)) Then
j = j + 1
dic.Add arr(i, 1), j
kq(1, j) = arr(i, 1)
End If
If Not dic.exists(arr(i, 2)) Then
j1 = j1 + 1
dic.Add arr(i, 2), j1
kq(j1, 1) = arr(i, 2)
End If
kq(dic.item(arr(i, 2)), dic.item(arr(i, 1))) = kq(dic.item(arr(i, 2)), dic.item(arr(i, 1))) + 1
Next i
Range("e1").Resize(j1, j) = kq
End Sub
2.Code tổng quát như sau:
Sub test()
Dim dic As Object, i As Long, arr As Variant, kq As Variant, j As Long, j1 As Long
Set dic = CreateObject("scripting.dictionary")
arr = Range("a1:b6").Value2
ReDim kq(1 To UBound(arr), 1 To 5)
j = 1
j1 = 1
For i = 1 To UBound(arr)
If Not dic.exists(arr(i, 1)) Then
j = j + 1
dic.Add arr(i, 1), j
kq(1, j) = arr(i, 1)
End If
If Not dic.exists(arr(i, 2)) Then
j1 = j1 + 1
dic.Add arr(i, 2), j1
kq(j1, 1) = arr(i, 2)
End If
kq(dic.item(arr(i, 2)), dic.item(arr(i, 1))) = kq(dic.item(arr(i, 2)), dic.item(arr(i, 1))) + 1
Next i
Range("e1").Resize(j1, j) = kq
End Sub