|
原帖由 shinying 于 2011-1-12 15:10 发表
怎么解决这个问题:
相同的PN的 ID太多,超过了256个字符,VBA会报错,怎么解决!
代码变成:
- Sub 按钮1_Click()
- Dim arr
- Dim rng1 As Range, rng2 As Range
- Dim dic As Object, dic1 As Object
- arr = Array("Pn", "id", "qty")
- Sheet2.Cells.ClearContents
- Sheet2.Range("a1:c1") = arr
- Set rng1 = Sheet1.Range(Cells(2, 1), Cells(Rows.Count, 1).End(3))
- Set dic = CreateObject("Scripting.Dictionary")
- Set dic1 = CreateObject("Scripting.Dictionary")
- For Each rng2 In rng1
- If dic.exists(rng2.Value) Then
- dic(rng2.Value) = dic(rng2.Value) & "," & rng2.Offset(, 1).Value
- dic1(rng2.Value) = dic1(rng2.Value) + 1
- Else
- dic(rng2.Value) = rng2.Offset(, 1).Value
- dic1(rng2.Value) = 1
- End If
- Next
- arr = dic.keys
- For x = 1 To dic.Count
- Sheet2.Range("a" & x + 1) = arr(x - 1)
- Sheet2.Range("b" & x + 1) = dic.Item(arr(x - 1))
- Sheet2.Range("c" & x + 1) = dic1.Item(arr(x - 1))
- Next
- Set dic = Nothing
- Set dic1 = Nothing
- End Sub
复制代码 |
|