UniqueID Description ConsolidatedText
Str1 Here is a sentence Here is a sentence
Str2 And another sentence. And another sentence. And some words
Str2 And some words
Str3 123 123
Str4 abc abc ###
Str4 ###
好的 - 我会再试一次。忽略以前相同标题和未格式化代码的帖子!!
我有许多记录(~4000),每个记录都有一个UniqueID值(文本)和一个文本字段(可能非常冗长),这是用户输入的数据描述。我需要通过将所有描述连接到一个记录来合并电子表格,其中有多个UniqueID值出现。通常,我想循环遍历潜在值的范围并说“如果UniqueID相等,则获取所有Description值并将它们连接在一起(第一行或新行)然后删除所有旧的行“。基本上,我想在此示例数据中创建ConsolidatedText字段,然后还删除额外的行。这超出了我的VBA编程能力,对此宏结构的任何帮助都将非常感激。
答案 0 :(得分:2)
尝试使用以下代码,它假定您有标题,并且该列中的唯一ID位于A列和说明中。
Option Explicit
Sub HTH()
Dim vData As Variant
Dim lLoop As Long
Dim strID As String, strDesc As String
'// Original data sheet, change codename to suit
vData = Sheet1.UsedRange.Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For lLoop = 1 To UBound(vData, 1)
strID = vData(lLoop, 1):strDesc = vData(lLoop, 2)
If Not .exists(strID) Then
.Add strID, strDesc
Else
.Item(strID) = .Item(strID) & " " & strDesc
End If
Next
'// Data output, change sheet codename to suit
Sheet2.Range("a1").Resize(.Count).Value = Application.Transpose(.keys)
Sheet2.Range("b1").Resize(.Count).Value = Application.Transpose(.items)
End With
End Sub
修改强>
如果要删除并覆盖原始数据,请尝试:
Option Explicit
Sub HTH()
Dim vData As Variant
Dim lLoop As Long
Dim strID As String, strDesc As String
'// Change all references of activesheet to your worksheet codename.
With ActiveSheet.UsedRange
vData = .Value
.Clear
End With
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For lLoop = 1 To UBound(vData, 1)
strID = vData(lLoop, 1):strDesc = vData(lLoop, 2)
If Not .exists(strID) Then
.Add strID, strDesc
Else
.Item(strID) = .Item(strID) & " " & strDesc
End If
Next
'// Data output, change sheet codename to suit
ActiveSheet.Range("a1").Resize(.Count).Value = Application.Transpose(.keys)
ActiveSheet.Range("b1").Resize(.Count).Value = Application.Transpose(.items)
End With
End Sub
答案 1 :(得分:0)
如果你不想做vba(如果这只是一次拍摄),你可以做以下事情:
=IF(A2=A3;B2&" "&B3;IF(A2=A1;"dupplicate";B2))
如果您有两个以上相同的ID,我会让您调整公式。