帮我写一段excel-visual-basic代码可以把B栏的中文名类似的名称归类在一起
Sub classifyNames() Dim lastRow As Long lastRow = Cells(Rows.Count, "B").End(xlUp).Row
Dim nameDict As Object
Set nameDict = CreateObject("Scripting.Dictionary")
Dim i As Long
For i = 2 To lastRow
Dim name As String
name = Trim(Cells(i, "B").Value)
If Len(name) > 0 Then
If Not nameDict.Exists(name) Then
nameDict.Add name, New Collection
End If
nameDict(name).Add i
End If
Next i
Dim j As Long
j = 2
For Each name In nameDict.Keys
Dim indices As Collection
Set indices = nameDict(name)
Dim k As Long
For k = 1 To indices.Count
Cells(j, "A").Value = name
Cells(j, "B").Value = Cells(indices(k), "B").Value
j = j + 1
Next k
Next name
End Sub
这段代码会将B栏中类似的中文名归类在一起,并将它们放在同一行中,第一列为名称,第二列为原始名称。请注意,这段代码需要使用Scripting.Dictionary对象,因此您需要在代码中添加Microsoft Scripting Runtime的引用。
原文地址: https://www.cveoy.top/t/topic/lVo 著作权归作者所有。请勿转载和采集!