合并具有相似数据的行,将差异值连接到单独的单元格中

时间:2016-05-17 14:14:51

标签: excel vba

我找到了类似的问题,但我有一点不同。

注意:这适用于数千个单元格。

我正在尝试合并类似的TimeDate / Productname / Username行,但是为它们自己的单元格提供区分标记值(TimeDate,Productname,Username和TagValue都是它们自己的单元格列。)

Image of what is required

更新:我合并了前三列,因此更容易比较。我有完成这项工作的代码,但将标记值组合到一个单元格而不是单独的单元格中。如何将此代码放在单独的相邻单元格中?

Sub CombineRows()
      'Update 20131202
Dim WorkRng As Range
Dim Dic As Variant
Dim arr As Variant
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
 Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address,    Type:=8)
Set Dic = CreateObject("Scripting.Dictionary")
  arr = WorkRng.Value
For i = 1 To UBound(arr, 1)
xvalue = arr(i, 1)
If Dic.Exists(xvalue) Then
    Dic(arr(i, 1)) = Dic(arr(i, 1)) & " " & arr(i, 2)
Else
    Dic(arr(i, 1)) = arr(i, 2)
End If
      Next
       Application.ScreenUpdating = False
       WorkRng.ClearContents
       WorkRng.Range("A1").Resize(Dic.Count, 1) =          Application.WorksheetFunction.Transpose(Dic.keys)
      WorkRng.Range("B1").Resize(Dic.Count, 1) =   Application.WorksheetFunction.Transpose(Dic.items)
      Application.ScreenUpdating = True
      End Sub

0 个答案:

没有答案