gpt4 book ai didi

excel - 从 Excel 中包含逗号分隔值的两个单元格中提取公共(public)值

转载 作者:行者123 更新时间:2023-12-04 21:09:45 26 4
gpt4 key购买 nike

有没有一种简单的方法可以从两个单元格中用逗号分隔的数字提取共同数字?

我有一些单元格,每个单元格中有 12 个逗号分隔的数字。 (它们并不都是唯一的。有些数字可以重复两次。但不会超过两次。数字都是正数,只有一位或两位数)

我的数据是这样的:它们在 A 列中:

11,11,13,15,16,18,20,20,26,27,28,29
8,9,10,12,13,14,18,20,21,22,24,28
13,13,14,14,15,17,18,19,20,21,23,25
6,6,8,10,12,14,15,17,18,20,20,25
11,13,17,18,19,19,22,25,26,28,28,31
7,9,15,16,17,18,23,24,24,25,26,27
7,9,11,12,12,15,16,16,18,18,20,23
9,11,13,15,18,22,23,24,25,28,29,29
7,9,10,11,12,12,13,14,15,16,19,22
5,10,11,12,12,16,17,18,20,22,24,25
7,10,13,16,16,17,18,19,21,23,24,24
10,14,16,18,18,19,21,23,23,25,27,28

我想要的结果是这样的:

enter image description here

我需要一个无需将值分隔到不同列中的解决方案。感谢您的帮助。

由于在某些情况下可能会有数字重复两次,所以我也对这样的解决方案持开放态度。

enter image description here

最佳答案

匹配子字符串

这是更简单的“重复”解决方案:

Excel 中这样使用:

=comStr(A2,A3)

将代码复制到标准模块中,例如模块 1

代码

Option Explicit

Function comStr(String1 As String, _
String2 As String, _
Optional ByVal Delimiter As String = ",") _
As String

Dim Data1, Data2, Result(), i As Long, j As Long, l As Long
Data1 = Split(String1, Delimiter)
Data2 = Split(String2, Delimiter)
For i = 0 To UBound(Data1)
For j = 0 To UBound(Data2)
If Data1(i) = Data2(j) Then GoSub writeResult: Exit For
Next j
Next i

comStr = Join(Result, Delimiter)

Exit Function

writeResult:
ReDim Preserve Result(l)
Result(l) = Data1(i)
l = l + 1
Return

End Function

编辑:

这是“完整”版本,您可以在其中选择是否允许重复。

Excel 中这样使用:

=comStr(A2,A3,TRUE) 允许重复(如上面的版本)或=comStr(A2,A3)=comStr(A2,A3,FALSE) 不允许它们。

Function comStr(String1 As String, _
String2 As String, _
Optional allowDupes As Boolean = False, _
Optional ByVal Delimiter As String = ",") _
As String

Dim Data1, Data2, Result(), Curr, i As Long, j As Long, l As Long, n As Long
Data1 = Split(String1, Delimiter)
Data2 = Split(String2, Delimiter)
For i = 0 To UBound(Data1)
Curr = Data1(i)
For j = 0 To UBound(Data2)
If Data2(j) = Curr Then GoSub writeResult: Exit For
Next j
Next i

If l = 0 Then Exit Function
comStr = Join(Result, Delimiter)

Exit Function

writeResult:
If Not allowDupes Then
If l > 0 Then
For n = 0 To l - 1
If Result(n) = Curr Then Exit For
Next
If n <= l - 1 Then Return
End If
End If
ReDim Preserve Result(l)
Result(l) = Data1(i)
l = l + 1
Return

End Function

关于excel - 从 Excel 中包含逗号分隔值的两个单元格中提取公共(public)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62726755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com