【问题标题】:Subtracting a comma separated string from another one in Excel [closed]在Excel中从另一个字符串中减去逗号分隔的字符串[关闭]
【发布时间】:2020-03-07 07:53:29
【问题描述】:

如何在 Excel 中从另一个字符串中减去逗号分隔的字符串?

  • 如果 A1:1,2,3,4,5,6,7,8,9
  • 和A2:2,6,9
  • 我想要的结果应该在单元格 A3 中,减法后 (A1-A2):1,3,4,5,7,8

A2=2,3,4(串行)通过SUBSTITUTE函数很容易,但是我找不到上述问题的解决方案。

【问题讨论】:

  • 嗨米兰,请考虑编辑您的问题以使其更好。不要使用全部大写,并详细说明您的问题。到目前为止,您尝试过什么?
  • 使用 VBA / UDF 会很容易 .. 以此为起点... stackoverflow.com/questions/60565666/…
  • 如果您有其他问题,请不要更改问题。而是发布另一个问题。如果需要,请参阅此问题。

标签: excel vba excel-formula


【解决方案1】:

您同时拥有标记公式和 VBA。所以让我也给你两个选择:


1)公式

=TEXTJOIN(",",1,FILTERXML("<t><s>"&SUBSTITUTE(A1&","&A2,",","</s><s>")&"</s></t>","//s[not(following::*=. or preceding::*=.)]"))

注1:这是一个数组公式,需要通过CtrlShiftEnter确认

注意2:这需要访问TEXTJOIN 函数,该函数在office 365 和Excel 2019 中可用。

注意3:更多有用的FILTERXML“技巧”可以找到here


2)UDF

我建议使用 SplitJoin 函数来执行此操作:

Function TxtFilter(val1 As String, val2 As String, sep As String) As String

Dim arr1 As Variant, arr2 As Variant
Dim x As Long
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")

arr1 = Split(val1, sep)
arr2 = Split(val2, sep)

'Iterate first array to load dictionary
For x = LBound(arr1) To UBound(arr1)
    dict(arr1(x)) = 1
Next x

'Iterate second array to remove from dictionary
For x = LBound(arr2) To UBound(arr2)
    If dict.Exists(arr2(x)) Then dict.Remove arr2(x)
Next x

'Join remainder back together
TxtFilter = Join(dict.keys, sep)

End Function

通过=TxtFilter(A1,A2,",")拨打任何单元格

【讨论】:

  • 太棒了……就像 UDF 一样
  • 是的,这让我很开心,我非常感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 2014-06-19
相关资源
最近更新 更多