Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1959|回复: 5

[已解决]用VBA去掉单元格内重复文本

[复制链接]
发表于 2013-4-3 06:21 | 显示全部楼层 |阅读模式
如何通过VBA代码去掉单元格内重复文本呢,见附件
最佳答案
2013-4-3 07:43
改进下,增加对str1的判断
  1. Function 去重复数字(str1 As String, Optional delimiter As String = "-") As String
  2.    
  3.     If Len(str1) = 0 Then Exit Function
  4.    
  5.     If Not str1 Like "*" & delimiter & "*" Then
  6.         Exit Function
  7.     End If

  8.     Dim arr
  9.     arr = Split(str1, delimiter)
  10.     Dim i As Integer

  11.     For i = UBound(arr) To LBound(arr) Step -1
  12.         If InStr(delimiter & 去重复数字, delimiter & arr(i) & delimiter) = 0 Then
  13.             去重复数字 = arr(i) & delimiter & 去重复数字
  14.         End If
  15.     Next
  16.    
  17.     If Len(去重复数字) > 1 Then
  18.        去重复数字 = (Left(去重复数字, Len(去重复数字) - 1))
  19.     End If
  20.    
  21. End Function
复制代码

Book1.zip

6.01 KB, 下载次数: 21

发表于 2013-4-3 07:15 | 显示全部楼层
回复

使用道具 举报

发表于 2013-4-3 07:32 | 显示全部楼层
  1. Function 去重复数字(str1 As String, Optional delimiter As String = "-") As String
  2.     If Not str1 Like "*" & delimiter & "*" Then
  3.         Exit Function
  4.     End If
  5.     Dim arr
  6.     arr = Split(str1, delimiter)
  7.     Dim i As Integer

  8.     For i = UBound(arr) To LBound(arr) Step -1
  9.         If InStr(delimiter & 去重复数字, delimiter & arr(i) & delimiter) = 0 Then
  10.             去重复数字 = arr(i) & delimiter & 去重复数字
  11.         End If
  12.     Next
  13.    
  14.     If Len(去重复数字) > 1 Then
  15.        去重复数字 = (Left(去重复数字, Len(去重复数字) - 1))
  16.     End If
  17. End Function
复制代码
回复

使用道具 举报

发表于 2013-4-3 07:43 | 显示全部楼层    本楼为最佳答案   
改进下,增加对str1的判断
  1. Function 去重复数字(str1 As String, Optional delimiter As String = "-") As String
  2.    
  3.     If Len(str1) = 0 Then Exit Function
  4.    
  5.     If Not str1 Like "*" & delimiter & "*" Then
  6.         Exit Function
  7.     End If

  8.     Dim arr
  9.     arr = Split(str1, delimiter)
  10.     Dim i As Integer

  11.     For i = UBound(arr) To LBound(arr) Step -1
  12.         If InStr(delimiter & 去重复数字, delimiter & arr(i) & delimiter) = 0 Then
  13.             去重复数字 = arr(i) & delimiter & 去重复数字
  14.         End If
  15.     Next
  16.    
  17.     If Len(去重复数字) > 1 Then
  18.        去重复数字 = (Left(去重复数字, Len(去重复数字) - 1))
  19.     End If
  20.    
  21. End Function
复制代码
回复

使用道具 举报

发表于 2013-4-3 09:11 | 显示全部楼层
  1. Function XX(R As Range, Optional K$ = "-") As String
  2.     If IsMissing(K) Then K = "-"
  3.     With CreateObject("vbscript.regexp")
  4.         .Global = True: .Pattern = "(" & K & "\d+)(?=.+?\1)"
  5.         If .test(K & R.Value) Then
  6.             XX = Mid(.Replace(K & R.Value, ""), 2)
  7.         End If
  8.     End With
  9.     Application.Volatile
  10. End Function
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-4-4 19:33 | 显示全部楼层
liuguansky 发表于 2013-4-3 09:11

这个代码有个缺陷,就是前两个数字重复时,结果返回为空
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-25 17:04 , Processed in 0.249836 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表