Excel formula 如何将两个单元的经验合并为一个

Excel formula 如何将两个单元的经验合并为一个,excel-formula,Excel Formula,我们已经从参与者那里收集了工作数据。现在我们有以下表格中的数据 经验-1(单元1)(5年4个月)经验-2(单元2)(4年9个月) 现在,我们要结合两个单元格的经验,计算总经验,比如10年1个月的经验,对于香草Excel公式来说,这有点困难。假设字符串位于单元格A2中,可以使用以下逻辑计算值: 可以使用以下公式查找第一个数字: =MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};A2&"0123456789")) =MAX(ISNUMBER(VALUE(MID(A2;{1

我们已经从参与者那里收集了工作数据。现在我们有以下表格中的数据

经验-1(单元1)(5年4个月)经验-2(单元2)(4年9个月)


现在,我们要结合两个单元格的经验,计算总经验,比如10年1个月的经验,对于香草Excel公式来说,这有点困难。假设字符串位于单元格
A2
中,可以使用以下逻辑计算值:
可以使用以下公式查找第一个数字:

=MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};A2&"0123456789"))
=MAX(ISNUMBER(VALUE(MID(A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})
您可以使用以下公式查找最后一个字符串:

=MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};A2&"0123456789"))
=MAX(ISNUMBER(VALUE(MID(A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})
假设您的申请者工作年限均未超过99年,则修剪即可:

=TRIM(MID($A2;MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};A2&"0123456789"));2))*1
年岁

=TRIM(MID($A2;MAX(ISNUMBER(VALUE(MID($A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})-1;2))
几个月。 为了得到一个可以求和的数字,将年份存储为整数,将月份除以12作为非整数部分

=TRIM(MID($A2;MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};$A2&"0123456789"));2))*1+TRIM(MID($A2;MAX(ISNUMBER(VALUE(MID($A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})-1;2))/12
这些数字可以相加,结果为
10.83333
。为了在末尾得到字符串,您需要在总和上使用一个公式(在我的示例单元格
X1
),例如


请注意,除非您的用户将数据保存为
0年6个月,否则您可能需要制定更复杂的公式。

对于普通Excel公式,这有点困难。假设字符串位于单元格
A2
中,可以使用以下逻辑计算值:
可以使用以下公式查找第一个数字:

=MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};A2&"0123456789"))
=MAX(ISNUMBER(VALUE(MID(A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})
您可以使用以下公式查找最后一个字符串:

=MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};A2&"0123456789"))
=MAX(ISNUMBER(VALUE(MID(A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})
假设您的申请者工作年限均未超过99年,则修剪即可:

=TRIM(MID($A2;MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};A2&"0123456789"));2))*1
年岁

=TRIM(MID($A2;MAX(ISNUMBER(VALUE(MID($A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})-1;2))
几个月。 为了得到一个可以求和的数字,将年份存储为整数,将月份除以12作为非整数部分

=TRIM(MID($A2;MIN(SEARCH({0;1;2;3;4;5;6;7;8;9};$A2&"0123456789"));2))*1+TRIM(MID($A2;MAX(ISNUMBER(VALUE(MID($A2;{1;2;3;4;5;6;7;8;9};1)))*{1;2;3;4;5;6;7;8;9})-1;2))/12
这些数字可以相加,结果为
10.83333
。为了在末尾得到字符串,您需要在总和上使用一个公式(在我的示例单元格
X1
),例如


请注意,除非您的用户将数据保存为
0年6个月
,保存期限小于1年,否则您可能需要制定更为复杂的公式。

使用用户定义的函数,可以更容易地避免长公式的主要缺点:

Function sumyearmonth(rng As Range) As String
Dim rng2 As Range, strArr() As String, runtotal As Double, outYear As Long, outMonth As Long

For Each rng2 In rng ' for each of the cells
    strArr = Split(rng2.Value2, " ") 'splits any sort of string by spaces
    For i = LBound(strArr) To UBound(strArr) ' for each element of the array
        Select Case True
            Case strArr(i) Like "*year*" Or strArr(i) Like "*Year*" 'if it says something like "year", "Year" or "Years"
                runtotal = runtotal + Val(strArr(i - 1)) 'then the running total gets incremented by the preceding item of the array, e.g. 12 (years)
            Case strArr(i) Like "*month*" Or strArr(i) Like "*Month*"  'if it says something like "month", "Month" or "Months"
                runtotal = runtotal + Val(strArr(i - 1)) / 12 'then the running total gets incremented by the preceding item of the array, e.g. 9 (months) /12
        End Select
    Next i
Next rng2
'now we just need to interpret the running total
outYear = Int(runtotal) 'years is the integer part of the running total
outMonth = Round((runtotal - outYear) * 12, 0) 'months is the noninteger part of the running total multiplied by 12 and rounded to the next integer
sumyearmonth = outYear & " Year" & IIf(outYear > 1, "s", "") 'if it's more than 1 year, then years
sumyearmonth = sumyearmonth & " " & outMonth & " Month" & IIf(outMonth > 1, "s", "") 'if it's more than 1 month, then months
End Function

此解决方案不假定输入为年和月,它有一个很好的输出格式[Year(s)],并且您不需要在旁边显示运行总数。

通过用户定义的函数,可以更容易地避免长公式的主要缺点:

Function sumyearmonth(rng As Range) As String
Dim rng2 As Range, strArr() As String, runtotal As Double, outYear As Long, outMonth As Long

For Each rng2 In rng ' for each of the cells
    strArr = Split(rng2.Value2, " ") 'splits any sort of string by spaces
    For i = LBound(strArr) To UBound(strArr) ' for each element of the array
        Select Case True
            Case strArr(i) Like "*year*" Or strArr(i) Like "*Year*" 'if it says something like "year", "Year" or "Years"
                runtotal = runtotal + Val(strArr(i - 1)) 'then the running total gets incremented by the preceding item of the array, e.g. 12 (years)
            Case strArr(i) Like "*month*" Or strArr(i) Like "*Month*"  'if it says something like "month", "Month" or "Months"
                runtotal = runtotal + Val(strArr(i - 1)) / 12 'then the running total gets incremented by the preceding item of the array, e.g. 9 (months) /12
        End Select
    Next i
Next rng2
'now we just need to interpret the running total
outYear = Int(runtotal) 'years is the integer part of the running total
outMonth = Round((runtotal - outYear) * 12, 0) 'months is the noninteger part of the running total multiplied by 12 and rounded to the next integer
sumyearmonth = outYear & " Year" & IIf(outYear > 1, "s", "") 'if it's more than 1 year, then years
sumyearmonth = sumyearmonth & " " & outMonth & " Month" & IIf(outMonth > 1, "s", "") 'if it's more than 1 month, then months
End Function

此解决方案不假定输入为年和月,它有一个很好的输出格式[Year(s)],并且您不需要在旁边显示运行总数。

假设体验的措辞总是以
x Year(s)y Month(s)
的形式出现,并且所有体验都列在如下列中:

假设您为上述列表指定了一个名为列表_Exp,您可以使用以下数组公式,需要在公式栏中按Ctrl+Shift+Enter确认,以首先查找总月数

=SUMPRODUCT(--TRIM(MID(SUBSTITUTE(List_Exp," ",REPT(" ",100)),{1,200},100)),IF(ROW($A$1:INDEX($A:$A,ROWS(List_Exp)))>0,{12,1}))
在这里,我使用TRIM+MID+SUBSTITUTE+REPT函数从原始字符串中提取年和月的数字,即
{5,4;4,9;1,2;1,1;10,9}
,从上述示例中提取,然后使用SUMPRODUCT通过找到相应的乘数
{12,1;12,1;12,1;12,1}计算出总月份数
使用IF+ROW+INDEX函数

然后您可以将总月数转换为年数月数,如下所示:

如果您不想在上述示例中使用辅助单元格(单元格
D3:D6
),可以将总月数命名为Ttl_Mth,并将以下公式合并为一个公式:

年份数字:
=INT(Ttl\u Mth/12)

年份词:
=“Year”&IF(INT(Ttl_Mth/12)>1,“s”和“)

月份数字:
=MOD(Ttl\u Mth,12)

月份词:
=“Month”&IF(MOD(Ttl_Mth,12)>1,“s”和“)

组合:

=INT(Ttl_Mth/12)&" "&"Year"&IF(INT(Ttl_Mth/12)>1,"s","")&" "&MOD(Ttl_Mth,12)&" "&"Month"&IF(MOD(Ttl_Mth,12)>1,"s","")
请参见下面的测试结果,了解新的体验列表:

[提示]我建议先将体验列表转换为表格,然后再为列表命名。好处是,每次添加或替换列表(实际上是一列表)中的值时,命名范围将自动更新,因此计算也将自动更新


假设经验的措辞总是以
x年y月的形式出现
,并且所有经验都列在一列中,如下所示:

假设您为上述列表指定了一个名为列表_Exp,您可以使用以下数组公式,需要在公式栏中按Ctrl+Shift+Enter确认,以首先查找总月数

=SUMPRODUCT(--TRIM(MID(SUBSTITUTE(List_Exp," ",REPT(" ",100)),{1,200},100)),IF(ROW($A$1:INDEX($A:$A,ROWS(List_Exp)))>0,{12,1}))
在这里,我使用TRIM+MID+SUBSTITUTE+REPT函数从原始字符串中提取年和月的数字,即
{5,4;4,9;1,2;1,1;10,9}
,从上述示例中提取,然后使用SUMPRODUCT通过找到相应的乘数
{12,1;12,1;12,1;12,1}计算出总月份数
使用IF+ROW+INDEX函数

然后您可以将总月数转换为年数月数,如下所示:

如果您不想在上述示例中使用辅助单元格(单元格
D3:D6
),可以将总月数命名为Ttl_Mth,并将以下公式合并为一个公式:

年份数字:
=INT(