VBA Excel两个单元格字符串(开头相同)交集

该文章介绍了如何使用Excel宏函数StringIntersect来计算两个字符串的交集,特别指出需启用宏并保存文件为.xlsm格式。示例展示了当s1为abc,s2为abcd时,结果为ab;当s1为123,s2为1时,结果为1。函数通过比较子串在主字符串中的位置找到交集。

这里写自定义目录标题

单元格字符串交集

  1. 功能:计算两个开头字符串相同的内容
  2. 注意:Excel要启用宏,且文件要保存为.xlsm格式
  3. 示例:
    a). StringIntersect(“abc”,“abcd”) 结果为:abc
    b). StringIntersect(“123”,“1”) 结果为:1
'字符串s2在s1中的的内容截取。s1:abc s2:abcd =》abc,s1:123 s2:1=>1
Function StringIntersect(s1 As String, s2 As String) As String
Dim strlen As Integer
Dim temLen As Integer
Dim chekV As Boolean
Dim temStr As String

chekV = True
strlen = Len(s2)
temLen = 0
Do
    temLen = temLen + 1
    temStr = Left(s2, temLen)
    If (InStr(s1, temStr) < 1) Then chekV = False
Loop Until chekV = False Or temLen > strlen
StringIntersect = Left(s2, temLen - 1)
End Function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值