Chuyển đổi Feet và Inch sang Feet thập phân - Mẹo Excel

Mục lục

Cập nhật tháng 3 năm 2017: Dan Ashby có sẵn chức năng feet-and-inch-in-excel được cải tiến.

Tôi có một bảng tính Excel với một cột có độ dài ở định dạng 12 '6 7/8 ". Làm cách nào để tôi có thể sử dụng Excel để chuyển giá trị này sang bộ thập phân? Tôi không muốn chia cột thành hai phần. Sau đó, làm cách nào tôi có thể chuyển đổi lại thành feet và inch trong Excel không?

Hàm tùy chỉnh feet () được hiển thị bên dưới sẽ lấy một trường văn bản ở định dạng mà bạn mô tả và chuyển nó thành feet thập phân.

Chức năng tùy chỉnh LenText sẽ chuyển đổi một số feet thập phân thành một trường văn bản hiển thị feet, inch và inch phân số chính xác đến 1/32 ".

Để nhập chức năng tùy chỉnh,

  • Khởi động trình soạn thảo VB với alt-F11.
  • Chèn> Mô-đun.
  • Chèn> Thủ tục.
  • Nhập feet làm tên của thủ tục và cho biết nó là một hàm.
  • Sau đó, sao chép mã sau:
Public Function feet(LenString As String) Dim FootSign As Integer Dim InchSign As Integer Dim SpaceSign As Integer Dim FracSign As Integer Dim InchString As String Dim Word2 As String ' Copyright 1999, 2005.com LenString = Application.WorksheetFunction.Trim(LenString) 'The find function returns an error when the target is not found 'Resume Next will prevent VBA from halting execution. On Error Resume Next FootSign = Application.WorksheetFunction.Find("'", LenString) If IsEmpty(FootSign) Or FootSign = 0 Then ' There are no feet in this expression feet = 0 FootSign = 0 Else feet = Val(Left(LenString, FootSign - 1)) End If ' Handle the case where the foot sign is the last character If Len(LenString) = FootSign Then Exit Function ' Isolate the inch portion of the string InchString = Application.WorksheetFunction.Trim(Mid(LenString, FootSign + 1)) ' Strip off the inch sign, if there is one InchSign = Application.WorksheetFunction.Find("""", InchString) If Not IsEmpty(InchSign) Or InchSign = 0 Then InchString = Application.WorksheetFunction.Trim(Left(InchString, InchSign - 1)) End If ' Do we have two words left, or one? SpaceSign = Application.WorksheetFunction.Find(" ", InchString) If IsEmpty(SpaceSign) Or SpaceSign = 0 Then ' There is only one word here. Is it inches or a fraction? FracSign = Application.WorksheetFunction.Find("/", InchString) If IsEmpty(FracSign) Or FracSign = 0 Then 'This word is inches feet = feet + Val(InchString) / 12 Else ' This word is fractional inches feet = feet + (Val(Left(InchString, FracSign - 1)) / Val(Mid(InchString, FracSign + 1))) / 12 End If Else ' There are two words here. First word is inches feet = feet + Val(Left(InchString, SpaceSign - 1)) / 12 ' Second word is fractional inches Word2 = Mid(InchString, SpaceSign + 1) FracSign = Application.WorksheetFunction.Find("/", Word2) If IsEmpty(FracSign) Or FracSign = 0 Then ' Return an error feet = "VALUE!" Else If FracSign = 0 Then feet = "VALUE!" Else feet = feet + (Val(Left(Word2, FracSign - 1)) / Val(Mid(Word2, FracSign + 1))) / 12 End If End If End If End Function

Lặp lại cho chức năng có tên LenText. Sử dụng mã này:

Public Function LenText(FeetIn As Double) ' This function will change a decimal number of feet to the text string ' representation of feet, inches, and fractional inches. ' It will round the fractional inches to the nearest 1/x where x is the denominator. ' Copyright 1999.com Denominator = 32 ' must be 2, 4, 8, 16, 32, 64, 128, etc. NbrFeet = Fix(FeetIn) InchIn = (FeetIn - NbrFeet) * 12 NbrInches = Fix(InchIn) FracIn = (InchIn - NbrInches) * Denominator Numerator = Application.WorksheetFunction.Round(FracIn, 0) If Numerator = 0 Then FracText = "" ElseIf InchIn>= (11 + (31.4999999 / 32)) Then NbrFeet = NbrFeet + 1 NbrInches = 0 FracText = "" ElseIf Numerator = Denominator Then NbrInches = NbrInches + 1 FracText = "" Else Do ' If the numerator is even, divide both numerator and divisor by 2 If Numerator = Application.WorksheetFunction.Even(Numerator) Then Numerator = Numerator / 2 Denominator = Denominator / 2 Else FracText = " " & Numerator & "/" & Denominator Exit Do End If Loop End If LenText = NbrFeet & "' " & NbrInches & FracText & """" End Function
Chuyển đổi Feet và Inch sang Feet thập phân

Cột A hiển thị văn bản gốc. Cột B chuyển đổi chính xác thành feet bất kỳ giá trị nào chứa feet, inch và / hoặc inch phân số.

Lưu ý rằng nếu bạn không bao gồm dấu chân, giá trị được giả định là inch. (hàng 12). Nếu phần phân số của inch không hợp lệ, Giá trị! được trả về (hàng 13).

Cập nhật: Cảm ơn Dale Richmond từ Thành phố Kansas, người đã cung cấp bản cập nhật vào tháng 10 năm 2007 để ngăn câu trả lời là 1 foot 12 inch khi một số lớn hơn 1,999 được chuyển đến hàm.

thú vị bài viết...