Excel公式,用于将包含周和日的文本转换为天

时间:2016-12-08 23:25:59

标签: excel function date time formula

我想要几天内的所有事情,假设1周= 5天。所以我有以下数据(也想忽略>和<标志):

  1w 3d
> 1w 4d
> 1w 3.25d
< 2w 3.25d
> 1w 0.75d
< 4.25d
  0d

希望答案如下:

8, Calculated like so --> ((1 week * 5) + 3 days)
9
8.25
13.25
5.75
4.25
0

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

考虑以下单行用户定义函数:

Public Function xvert(sin As String) As Double
   While Not Left(sin, 1) Like "[0-9]"
       sin = Mid(sin, 2)
   Wend
   xvert = Evaluate(Replace(Replace(Replace(sin, " ", "+"), "d", ""), "w", "*5"))
End Function

enter image description here

UDF的工作原理是将数据转换为公式,然后评估公式。所以:

3w 4.3d

评估为:

= 3 * 5 + 4.3

用户定义函数(UDF)非常易于安装和使用:

  1. ALT-F11调出VBE窗口
  2. ALT-I ALT-M打开了一个新模块
  3. 粘贴内容并关闭VBE窗口
  4. 如果保存工作簿,UDF将随之保存。 如果您在2003年之后使用的是Excel版本,则必须保存 该文件为.xlsm而不是.xlsx

    删除UDF:

    1. 按上述方式调出VBE窗口
    2. 清除代码
    3. 关闭VBE窗口
    4. 从Excel使用UDF:

      = myfunction的(A1)

      要了解有关宏的更多信息,请参阅:

      http://www.mvps.org/dmcritchie/excel/getstarted.htm

      http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

      有关UDF的详细信息,请参阅:

      http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

      必须启用宏才能使其正常工作!

答案 1 :(得分:1)

对于公式方法:

=IFERROR(TRIM(SUBSTITUTE(SUBSTITUTE(LEFT(A1,FIND("w",A1)-1),">",""),"<","")),0)*5 + TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(MID(A1,IFERROR(FIND("w",A1),0)+1,LEN(A1)),">",""),"<",""),"d",""))

enter image description here

修改

根据你的评论,这个公式应该处理这个问题。

=IFERROR(TRIM(SUBSTITUTE(SUBSTITUTE(LEFT(A1,FIND("w",A1)-1),">",""),"<","")),0)*5 + IFERROR(--TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(MID(A1,IFERROR(FIND("w",A1),0)+1,LEN(A1)),">",""),"<",""),"d","")),0)