我希望能够以0-00格式输入英尺和英寸,增量为1/4。
示例:
1' 3" = 1-03
2' 5 1/4" = 2-051
10	 9 1/2" = 10-092
22' 10 3/4" = 22-103
我有一个用户表单设置,我希望用户能够像上面那样输入维度。但是,我的所有公式都设置为英尺和十进制英寸。因此,在用户输入数据后,我需要将其转换为十进制数。
答案 0 :(得分:0)
将输入解析为文本...
所以,例如,2-051 =
2
+ 05/12 = 0.41666
+ (1/4)/12 = .020833
= 2.4375
在VBA中,此功能如下所示:
Function ParseHeight(ht As String) As Double
Dim Feet As Integer
Dim Inches As Integer
Dim Quarters As Integer
Dim DashLocation As Integer
DashLocation = InStr(1, ht, "-")
Feet = Mid(ht, 1, DashLocation - 1)
Inches = Mid(ht, DashLocation + 1, 2)
If Len(ht) = DashLocation + 3 Then Quarters = Mid(ht, DashLocation + 3, 1)
ParseHeight = Feet + (Inches / 12) + ((Quarters / 4) / 12)
End Function
鉴于您希望通过Excel中的公式执行此操作:
(假设在E1
输入,请在F1中尝试此公式):
=LEFT(E1,FIND("-",E1)-1)+MID(E1,FIND("-",E1)+1,2)/12+IFERROR((MID(E1,FIND("-",E1)+3,1)/4)/12,0)
然后,要将一个数字从十进制转换回你的格式,这样就可以了:
(假设要转换的小数在F1中):
=INT(F1)&"-"&TEXT(INT(MOD(F1,1)*12),"00")&INT(MOD(MOD(F1,1)*12,1)*4)