文章预览
学习Excel技术,关注微信公众号: excelperfect 标签: VBA , 自定义函数 如下图 1 所示,在单元格 A1 中包含多行文本,现在要求删除这些文本中开头单词相同的行,仅保留第 1 行,结果如图 1 单元格 B1 中所示。 图 1 可以使用 VBA 编写一个自定义函数来实现。 打开 VBE ,插入一个标准模块,输入代码: Public Function fnUnique(rng As Range) Dim dict As Object Dim dOut As Object Dim val As Variant Dim var As Variant Dim i As Integer Dim sKey As String Set dict = CreateObject( "scripting.dictionary" ) Set dOut = CreateObject( "scripting.dictionary" ) dict.CompareMode = vbTextCompare dOut.CompareMode = vbTextCompare val = rng.Value & "" var = Split (val, Chr ( 10 )) On Error Resume Next For i = 0 To UBound( var ) sKey = Split ( var (i))( 0 ) If dict.exists(sKey) = False Then dict.Add Key :=sKey, Item:= var (i) Else d
………………………………