|
|
- ' 可以合并多个内容
- Sub Demo()
- Dim objDic As Object, rngData As Range
- Dim i As Long, j As Long, sKey, aRow()
- Dim arrData, arrRes
- Set objDic = CreateObject("scripting.dictionary")
- Set rngData = Range("A1").CurrentRegion
- arrData = rngData.Value
- Dim ColCnt As Long: ColCnt = UBound(arrData, 2)
- For i = LBound(arrData) + 1 To UBound(arrData)
- sKey = arrData(i, 1)
- If Not objDic.exists(sKey) Then
- ReDim aRow(1 To ColCnt - 1)
- objDic(sKey) = aRow
- End If
- aRow = objDic(sKey)
- For j = 2 To ColCnt
- If Len(arrData(i, j)) > 0 Then
- If Len(aRow(j - 1)) = 0 Then
- aRow(j - 1) = arrData(i, j)
- Else
- aRow(j - 1) = aRow(j - 1) & "," & arrData(i, j)
- End If
- End If
- Next
- objDic(sKey) = aRow
- Next i
- ReDim arrRes(1 To objDic.Count + 1, 1 To ColCnt)
- For j = 1 To ColCnt
- arrRes(1, j) = arrData(1, j)
- Next
- i = 1
- For Each sKey In objDic.keys
- i = i + 1
- arrRes(i, 1) = sKey
- aRow = objDic(sKey)
- For j = 2 To ColCnt
- arrRes(i, j) = aRow(j - 1)
- Next
- Next
- Range("P1").Resize(UBound(arrRes), ColCnt).Value = arrRes
- End Sub
复制代码
|
|