2016-07-14 55 views

回答

0

正如布賴恩戴維斯提到的,你可以用公式來做到這一點。

假設初始文本總是格式化爲你在你的圖像呈現,下面的公式應該工作: =(LEFT(A1,FIND("hr",A1)-1)*60)+MID(A1,FIND("hr",A1)+3,3) & " mins"

當然,如果你堅持使用宏,下面的代碼可以用來轉換活動單元格(宏工作在其接受更靈活的格式感更好(如「1個hr10mins」作品與宏,但不與公式)):

Sub ConvertToMins() 
    Dim mins As Integer 
    Dim SearchString As String 
    Dim hourLength As Integer 
    Dim minPos As Integer 

    mins = 0 
    SearchString = ActiveCell.Text 

    If InStr(1, SearchString, " h", vbTextCompare) <> 0 Then 
     hourLength = InStr(1, SearchString, " h", vbTextCompare) - 1 
    ElseIf InStr(1, SearchString, "h", vbTextCompare) <> 0 Then 
     hourLength = InStr(1, SearchString, "h", vbTextCompare) - 1 
    Else 
     Exit Sub 
    End If 

    mins = mins + (CDbl(Left(SearchString, hourLength)) * 60) 

    If InStr(1, SearchString, " m", vbTextCompare) <> 0 Then 
     minPos = InStr(1, SearchString, " m", vbTextCompare) - 2 
    ElseIf InStr(1, SearchString, "m", vbTextCompare) <> 0 Then 
     minPos = InStr(1, SearchString, "m", vbTextCompare) - 2 
    Else 
     ActiveCell.Value = CStr(mins) & " mins" 
     Exit Sub 
    End If 

    If IsNumeric(Mid(SearchString, minPos, 2)) = True Then 
     mins = mins + (CInt(Mid(SearchString, minPos, 2))) 
    Else 
     mins = mins + (CInt(Mid(SearchString, minPos, 1))) 
    End If 

    ActiveCell.Value = CStr(mins) & " mins" 
End Sub 
+0

謝謝你的工作很酷....但thng是我想要它在C2:C22範圍內的所有單元格中循環遍歷我的所有工作簿工作簿此代碼爲nly活動單元格工作。 @Zachary Lim –

相關問題