|
例9中,如果原始表格如附件中排列,运行结果就不正确了,我改了代码
- Sub 条件合并解法1()
- Dim Dic As Object
- Dim RowN As Long, i As Long, CountN As Long, j As Long
- Dim arr1, arr2, arr3()
- Set Dic = CreateObject("scripting.dictionary")
- RowN = Sheet1.Range("a" & Rows.Count).End(xlUp).Row
- arr1 = Sheet1.Range("a2:f" & RowN)
- For i = 1 To UBound(arr1)
- Dic.Item(arr1(i, 2)) = ""
- Next
- arr2 = Dic.Keys
- CountN = Dic.Count
- Dic.RemoveAll
- ReDim arr3(1 To CountN, 1 To 6)
- For i = 1 To UBound(arr3)
- arr3(i, 2) = arr2(i - 1)
- For j = 1 To UBound(arr1)
- If arr1(j, 2) = arr3(i, 2) Then
- arr3(i, 1) = IIf(arr3(i, 1) = "", arr1(j, 1), arr3(i, 1) & "," & arr1(j, 1))
- arr3(i, 3) = arr1(j, 3)
- arr3(i, 4) = IIf(arr3(i, 4) = "", arr1(j, 4), arr3(i, 4) & "," & arr1(j, 4))
- arr3(i, 5) = arr3(i, 5) + Val(arr1(j, 5))
- arr3(i, 6) = arr3(i, 6) + Val(arr1(j, 6))
- End If
- Next
- Next
- Sheet2.Range("a1").Resize(1, 6) = Array("cmp", "name", "企业类型", "所属区县", "09一般贸易", "0910一般贸易")
- Sheet2.Range("a2").Resize(CountN, 6) = arr3
- End Sub
复制代码
|
|