2017-08-04 151 views
3

我有與存儲在以下格式高度數據幾列的數據庫小數英尺...轉換英尺和英寸(例如,「5英尺1」),以使用VBA或Excel

8英尺4在

5英尺1

8英尺6

12英尺0

等等...爲20K +記錄在3列。我需要使用Excel公式或VBA模塊將其轉換爲小數點。我已經適應另一個VBA腳本我用來轉換度分秒上市爲十進制度座標,我不確定我要去哪裏錯了...

Function ConvertFtInches(pInput As String) As Double 
'Updateby20140227 
Dim xFt As Double 
Dim xIn As Double 
xFt = Val(Left(pInput, InStr(1, pInput, " ft") - 1)) 
xIn = Val(Mid(pInput, InStr(1, pInput, " ft") + 2, _ 
      InStr(1, pInput, " in") - InStr(1, pInput, _ 
      " ft") - 2))/12 
ConvertFtInches = xFt + xIn 
End Function 

....我可以完全因爲我是VBA的初學者。劇本肯定有問題,因爲它是從不同腳本改編而來的。可能有一個更容易使用的Excel公式。

希望有人可以提供幫助。所有其他線程都包含未格式化爲與我的格式相同的值的轉換。謝謝!

回答

4

一個襯墊VBA:

Function ConvertFtInches(pInput As String) As Double 
ConvertFtInches = Split(pInput, " ft ")(0) + Split(Split(pInput, " ft ")(1), " in")(0)/12 
End Function 
+1

這就是爲什麼我仍然給你一個'+ 1' –

+0

@Scott Craner你+1是+1,謝謝 – Tehscript

+0

漂亮,謝謝! – BWasse

1

這裏有一個Excel公式,將工作:

=VALUE(LEFT(A1,FIND("ft",A1)-1))+(VALUE(SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND(" ",A1)),"in",""))/12) 

enter image description here

+1

IMO簡單:'= 1 * SUBSTITUTE(SUBSTITUTE(A19, 「中」, 「/ 12」),」英尺「,」「)'(假設緊接在'ft'之前的空格),但我認爲兩者都只有英寸失敗。 – pnuts

4

如果你想VBA:

Function ConvertFtInches(pInput As Range) As Double 

Dim str As String 
Dim strArr() As String 
str = Replace(pInput.Value, "ft", " ") 
str = Replace(str, "in", "") 
strArr = Split(Application.Trim(str), " ") 
If LBound(strArr) = UBound(strArr) Then 
    ConvertFtInches = strArr(0)/12 
Else 
    ConvertFtInches = strArr(0) + strArr(1)/12 

End If   

End Function 

enter image description here

+1

正準備修復VBA。 :)你的解決方案與'split'很好。 –

+0

當'4 in' +1 – Tehscript

+0

完美時不失敗!謝謝! – BWasse

0

能應付只有幾英寸(即不將它們轉換成日期!):

=IFERROR(LEFT(A1,FIND(" ft",A1)),0)+LEFT(RIGHT(A1,5),2)/12 
相關問題