Vba 如何使用公式在Excel中分离单元格值并进行比较?
我有两个值(A和B),如下图所示。我想用A和B计算下面提到的值Vba 如何使用公式在Excel中分离单元格值并进行比较?,vba,excel,split,compare,Vba,Excel,Split,Compare,我有两个值(A和B),如下图所示。我想用A和B计算下面提到的值 公共:A和B中公共值的百分比(例如,在第一个示例中只有4个公共值。因此,百分比为1/4) 仅A:仅A项目的百分比(例如,在第一个示例1中,5仅在A中。因此百分比为2/4) 仅B:仅B项的百分比(例如,在第一个示例中,6仅在B中,因此百分比为1/4) 我找不到合适的excel公式将一个单元格中的数字除以逗号并与下一列单元格进行比较(例如,1,4,5与4,6)。请告诉我,如果有任何具体的ms excel公式,我可以用于此 如果需要,我
如果需要,我很乐意提供更多详细信息:)这是一个针对常见问题的解决方案。在标准模块中输入以下自定义项:
Public Function Kommon(s1 As String, s2 As String) As Double
Dim arry1, arry2, C As Collection
arry1 = Split(s1, ",")
arry2 = Split(s2, ",")
Set C = New Collection
On Error Resume Next
For Each a In arry1
C.Add a, CStr(a)
Next a
For Each a In arry2
C.Add a, CStr(a)
Next a
On Error GoTo 0
cc = CDbl(C.Count)
For Each a In arry1
For Each b In arry2
If a = b Then
k = k + 1
Exit For
End If
Next b
Next a
Kommon = k / cc
End Function
然后按如下方式使用:
其他两种情况都是这种情况的微小变化。这里有一种解决方案,适用于常见的。在标准模块中输入以下自定义项:
Public Function Kommon(s1 As String, s2 As String) As Double
Dim arry1, arry2, C As Collection
arry1 = Split(s1, ",")
arry2 = Split(s2, ",")
Set C = New Collection
On Error Resume Next
For Each a In arry1
C.Add a, CStr(a)
Next a
For Each a In arry2
C.Add a, CStr(a)
Next a
On Error GoTo 0
cc = CDbl(C.Count)
For Each a In arry1
For Each b In arry2
If a = b Then
k = k + 1
Exit For
End If
Next b
Next a
Kommon = k / cc
End Function
然后按如下方式使用:
其他两种情况是这种情况的微小变化。使用带有选项参数的UDF进行输出计算
注意事项:
Option Explicit
Public Function GetPercentage(ByRef rng1 As Range, ByRef rng2 As Range, ByVal calcOption As String) As Double
Application.Volatile
'calcOption C = Common , A is a only, B is B only.
Dim arr1() As String, arr2() As String, totalAItems As Long, totalBItems As Long, totalItems As Long
arr1 = Split(rng1.Value, ",")
arr2 = Split(rng2.Value, ",")
totalAItems = GetDistinctCount(arr1)
totalBItems = GetDistinctCount(arr2)
totalItems = GetDistinctCount(Split(rng1.Value & "," & rng2.Value, ","))
Dim commonItemCount As Long
commonItemCount = GetSharedCount(arr1, arr2)
Select Case calcOption
Case "C"
GetPercentage = commonItemCount / totalItems
Case "A"
GetPercentage = OnlyInOneCell(arr1, arr2) / totalItems
Case "B"
GetPercentage = OnlyInOneCell(arr2, arr1) / totalItems
End Select
End Function
Public Function GetDistinctCount(ByVal arr As Variant) As Long
Dim tempDict As Object, i As Long
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr) To UBound(arr)
If Not tempDict.Exists(arr(i)) Then tempDict.Add arr(i), arr(i)
Next i
GetDistinctCount = tempDict.Count
End Function
Public Function GetSharedCount(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long
For i = LBound(arr1) To UBound(arr1)
If Not IsError(Application.Match(arr1(i), arr2, 0)) Then outCount = outCount + 1
Next i
GetSharedCount = outCount
End Function
Public Function OnlyInOneCell(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long, tempDict As Object
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr1) To UBound(arr1)
If IsError(Application.Match(arr1(i), arr2, 0)) Then
If Not tempDict.Exists(arr1(i)) Then tempDict.Add arr1(i), arr1(i)
End If
Next i
OnlyInOneCell = tempDict.Count
End Function
函数获取百分比
。根据传入的选项参数返回百分比
Arg1:rng1
范围对象1,例如单元格A2
Arg2:rng2
范围对象2,例如单元格B2
Arg3:calcOption
<代码>“C”=普通,“A”
仅为A,“B”
仅为B
代码:
Option Explicit
Public Function GetPercentage(ByRef rng1 As Range, ByRef rng2 As Range, ByVal calcOption As String) As Double
Application.Volatile
'calcOption C = Common , A is a only, B is B only.
Dim arr1() As String, arr2() As String, totalAItems As Long, totalBItems As Long, totalItems As Long
arr1 = Split(rng1.Value, ",")
arr2 = Split(rng2.Value, ",")
totalAItems = GetDistinctCount(arr1)
totalBItems = GetDistinctCount(arr2)
totalItems = GetDistinctCount(Split(rng1.Value & "," & rng2.Value, ","))
Dim commonItemCount As Long
commonItemCount = GetSharedCount(arr1, arr2)
Select Case calcOption
Case "C"
GetPercentage = commonItemCount / totalItems
Case "A"
GetPercentage = OnlyInOneCell(arr1, arr2) / totalItems
Case "B"
GetPercentage = OnlyInOneCell(arr2, arr1) / totalItems
End Select
End Function
Public Function GetDistinctCount(ByVal arr As Variant) As Long
Dim tempDict As Object, i As Long
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr) To UBound(arr)
If Not tempDict.Exists(arr(i)) Then tempDict.Add arr(i), arr(i)
Next i
GetDistinctCount = tempDict.Count
End Function
Public Function GetSharedCount(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long
For i = LBound(arr1) To UBound(arr1)
If Not IsError(Application.Match(arr1(i), arr2, 0)) Then outCount = outCount + 1
Next i
GetSharedCount = outCount
End Function
Public Function OnlyInOneCell(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long, tempDict As Object
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr1) To UBound(arr1)
If IsError(Application.Match(arr1(i), arr2, 0)) Then
If Not tempDict.Exists(arr1(i)) Then tempDict.Add arr1(i), arr1(i)
End If
Next i
OnlyInOneCell = tempDict.Count
End Function
表中的自定义项
使用带有选项参数的自定义项进行输出计算
注意事项:
Option Explicit
Public Function GetPercentage(ByRef rng1 As Range, ByRef rng2 As Range, ByVal calcOption As String) As Double
Application.Volatile
'calcOption C = Common , A is a only, B is B only.
Dim arr1() As String, arr2() As String, totalAItems As Long, totalBItems As Long, totalItems As Long
arr1 = Split(rng1.Value, ",")
arr2 = Split(rng2.Value, ",")
totalAItems = GetDistinctCount(arr1)
totalBItems = GetDistinctCount(arr2)
totalItems = GetDistinctCount(Split(rng1.Value & "," & rng2.Value, ","))
Dim commonItemCount As Long
commonItemCount = GetSharedCount(arr1, arr2)
Select Case calcOption
Case "C"
GetPercentage = commonItemCount / totalItems
Case "A"
GetPercentage = OnlyInOneCell(arr1, arr2) / totalItems
Case "B"
GetPercentage = OnlyInOneCell(arr2, arr1) / totalItems
End Select
End Function
Public Function GetDistinctCount(ByVal arr As Variant) As Long
Dim tempDict As Object, i As Long
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr) To UBound(arr)
If Not tempDict.Exists(arr(i)) Then tempDict.Add arr(i), arr(i)
Next i
GetDistinctCount = tempDict.Count
End Function
Public Function GetSharedCount(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long
For i = LBound(arr1) To UBound(arr1)
If Not IsError(Application.Match(arr1(i), arr2, 0)) Then outCount = outCount + 1
Next i
GetSharedCount = outCount
End Function
Public Function OnlyInOneCell(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long, tempDict As Object
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr1) To UBound(arr1)
If IsError(Application.Match(arr1(i), arr2, 0)) Then
If Not tempDict.Exists(arr1(i)) Then tempDict.Add arr1(i), arr1(i)
End If
Next i
OnlyInOneCell = tempDict.Count
End Function
函数获取百分比
。根据传入的选项参数返回百分比
Arg1:rng1
范围对象1,例如单元格A2
Arg2:rng2
范围对象2,例如单元格B2
Arg3:calcOption
<代码>“C”=普通,“A”
仅为A,“B”
仅为B
代码:
Option Explicit
Public Function GetPercentage(ByRef rng1 As Range, ByRef rng2 As Range, ByVal calcOption As String) As Double
Application.Volatile
'calcOption C = Common , A is a only, B is B only.
Dim arr1() As String, arr2() As String, totalAItems As Long, totalBItems As Long, totalItems As Long
arr1 = Split(rng1.Value, ",")
arr2 = Split(rng2.Value, ",")
totalAItems = GetDistinctCount(arr1)
totalBItems = GetDistinctCount(arr2)
totalItems = GetDistinctCount(Split(rng1.Value & "," & rng2.Value, ","))
Dim commonItemCount As Long
commonItemCount = GetSharedCount(arr1, arr2)
Select Case calcOption
Case "C"
GetPercentage = commonItemCount / totalItems
Case "A"
GetPercentage = OnlyInOneCell(arr1, arr2) / totalItems
Case "B"
GetPercentage = OnlyInOneCell(arr2, arr1) / totalItems
End Select
End Function
Public Function GetDistinctCount(ByVal arr As Variant) As Long
Dim tempDict As Object, i As Long
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr) To UBound(arr)
If Not tempDict.Exists(arr(i)) Then tempDict.Add arr(i), arr(i)
Next i
GetDistinctCount = tempDict.Count
End Function
Public Function GetSharedCount(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long
For i = LBound(arr1) To UBound(arr1)
If Not IsError(Application.Match(arr1(i), arr2, 0)) Then outCount = outCount + 1
Next i
GetSharedCount = outCount
End Function
Public Function OnlyInOneCell(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim outCount As Long, i As Long, tempDict As Object
Set tempDict = CreateObject("Scripting.Dictionary")
For i = LBound(arr1) To UBound(arr1)
If IsError(Application.Match(arr1(i), arr2, 0)) Then
If Not tempDict.Exists(arr1(i)) Then tempDict.Add arr1(i), arr1(i)
End If
Next i
OnlyInOneCell = tempDict.Count
End Function
表中的自定义项
“数据”选项卡下的“文本到列”将按逗号分隔单元格。或者,使用VBA:以及“数据”选项卡下的“文本到列”将按逗号分隔单元格。或者,使用VBA:以及。我设法运行了它。再次非常感谢你伟大的回答。非常有用:)我设法运行了它。再次非常感谢你伟大的回答。非常有用:)