excel比较并合并工作表( 二 )


Application.ScreenUpdating = False
Set rng = rngInput.Columns(ColIndex)
Set dic = New Scripting.Dictionary
‘文本比较,不区分大小写
dic.CompareMode = TextCompare
‘是否有标题
If blnHeaders Then
With rngInput
Set rngInput = .Offset(1,0).Resize( _
.Rows.Count – 1, .Columns.Count)
End With
End If
With rngInput
For Each cell In.Columns(ColIndex).Cells
i = i + 1
strVal = cell.Text
If Not dic.Exists(strVal) Then
dic.Add strVal, .Rows(i)
Else
‘将前几列具有相同数据的行存储在同一字典键
Set rngTemp = Union(.Rows(i),dic(strVal))
dic.Remove strVal
dic.Add strVal, rngTemp
End If
Next cell
End With
Set DicData = https://www.i8ku.com/2021/dic
Application.ScreenUpdating = True
End Function
运行代码后,即可得到上图3所示的结果 。
代码的图片版如下:
【excel比较并合并工作表】

excel比较并合并工作表

文章插图

推荐阅读