Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从文本形式的时间中获取平均值_Excel_Vba_Excel Formula - Fatal编程技术网

Excel 从文本形式的时间中获取平均值

Excel 从文本形式的时间中获取平均值,excel,vba,excel-formula,Excel,Vba,Excel Formula,我需要从一列中找出平均时间。时间目前为文本格式,即1天3小时3分3秒,因此我无法使用平均公式。 我怎样才能轻松地将其转换为一种格式,从而找到整个列的平均值? 我希望这是有道理的。 谢谢 可以使用FILTERXML: =INT(SUM(IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(LOWER(A1:A6)," ","</s><s>")&"

我需要从一列中找出平均时间。时间目前为文本格式,即1天3小时3分3秒,因此我无法使用平均公式。 我怎样才能轻松地将其转换为一种格式,从而找到整个列的平均值? 我希望这是有道理的。 谢谢

可以使用FILTERXML:

=INT(SUM(IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(LOWER(A1:A6)," ","</s><s>")&"</s></t>","//s[contains(.,"&{"'day'","'hour'","'min'","'sec'"}&")]/preceding-sibling::*[1]")/{1,24,1440,86400},0))/COUNTA(A1:A6))&" day(s) "&TEXT(SUM(IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(LOWER(A1:A6)," ","</s><s>")&"</s></t>","//s[contains(.,"&{"'day'","'hour'","'min'","'sec'"}&")]/preceding-sibling::*[1]")/{1,24,1440,86400},0))/COUNTA(A1:A6),"H \h\o\u\r\s m \m\i\n\u\t\e\s s \s\e\c\o\n\d\s")
根据不同的版本,在退出编辑模式时,可能需要使用Ctrl-Shift-Enter而不是Enter来确认


您可以将这些字符串转换为真实的日期/时间值:

t=1天3小时3分3秒 n=拆分trimt 总时间=时间序列0*24+n2、n4、n7 '总时间->1899-12-31 03:03:03 这些值可以取平均值

最后,如果希望显示时间格式,请应用此功能:

公共函数格式HourminuteSecond_ ByVal datTime作为日期_ 可选的ByVal stresparator作为字符串=:_ 作为字符串 '返回datTime的天、小时、分钟和秒数 '转换为小时、分钟和秒作为格式化字符串 '带有可选的时间分隔符选择。 ' 例如: '日期时间:10:03:55+20:01:24 '返回时间:30:05:19 ' ' 2014-06-17. 仙人掌数据ApS,CPH。 暗弦 作为字符串的Dim strMinuteSec 暗弦 strHour=CStrFixdatTime*24+小时时间 '必要时将前导零添加到分钟和秒计数。 strMinuteSec=Right0&CSTRMINUTEDATIME,2&STRESPARATOR&Right0&CSTRSECONDDATIME,2 strHours=strHour&STRESPARATOR&strMinuteSec FormatHourMinuteSecond=strHours 端函数 另一个依赖于字符串的VBA UDF解决方案始终具有以下格式:

x day y hours z minutes and n seconds
当单元格格式为[hh]:mm:ss时,此转换

1天3小时3分3秒至27:03:03

此格式可以很容易地求出平均值,以给出最终答案


如果没有时间怎么办?这会显示0分钟吗?还是跳过那部分?在VBAIf中,这将更容易如果没有分钟,它将显示为0分钟输出是什么样子的?输出将是原始的x天y小时z分钟v秒格式当您单击A2时,公式栏中有什么?谢谢!我已经更改了单元格范围,但我似乎只得到了0天0小时0分0秒。知道为什么吗?看了数据就知道了。你能编辑你的文章,包括一张数据模型的照片,显示公式和结果吗?
Public Function cst_time(Target As Range) As Double

Dim Temp, days As Long, hours As Double, minutes As Double, seconds As Double

Temp = Split(Target, " ")
days = Temp(0)
hours = Temp(2) / 24
minutes = Temp(4)
seconds = Temp(7) / 60

cst_time = days + hours + ((seconds + minutes) / (24 * 60))

End Function