|
Sub 时间转化()
Dim i As Integer
For i = 2 To 401
If InStr(Cells(i, 7), "天") > 0 Then
Cells(i, 12).Formula = _
"=left(g2,find(""天"",g2)-1)*24*3600 + mid(g2,find(""天"",g2)+1,find(""小"",g2)-find(""天"",g2)-1)*3600 + mid(g2,find(""时"",g2)+1,find(""分"",g2)-find(""时"",g2)-1)*60 + mid(g2,find(""分"",g2)+1,len(g2)-find(""分"",g2)-1)"
ElseIf InStr(Cells(i, 7), "天") = 0 And InStr(Cells(i, 7), "小时") > 0 Then
Cells(i, 12).Formula = _
"=left(g2,find(""小"",g2)-1)*3600 + mid(g2,find(""时"",g2)+1,find(""分"",g2)-find(""时"",g2)-1)*60 + mid(g2,find(""分"",g2)+1,len(g2)-find(""分"",g2)-1)"
ElseIf InStr(Cells(i, 7), "小时") = 0 And InStr(Cells(2, 7), "分") > 0 Then
Cells(i, 12).Formula = _
"=left(g2,find(""分"",g2)-1)*60 + mid(g2,find(""分"",g2)+1,len(g2)-find(""分"",g2)-1)"
ElseIf InStr(Cells(i, 7), "分") = 0 And InStr(Cells(i, 7), "分") > 0 Then
Cells(i, 12).Formula = "=left(g2,find(""秒"",g2)-1)"
End If
Next i
End Sub
目的是为了把导出的时长转化为秒,但由于导出的时长格式有xx天xx小时xx分xx秒,xx小时xx分xx秒,xx分xx秒,xx秒四种,如果只用Excel写的话每次都要筛选和写3次公式,很麻烦 所以想用vba一次性解决,以上代码能实现判断,但是公式里的g2行号不会变化,想请大虾帮忙解决一下问题,困扰一天了!
|
|