How can I convert numbers with “subscript” fractions to decimal in Excel?

懵懂的女人 提交于 2019-12-23 17:47:06

问题


I have thousands of numbers that were given to me in an unusable format for calculations:

9⅝, 9¼, 9¾, 10¼

This is how they appear in each row in Excel. I would like to convert these to a decimal. I've tried =FIXED(A2, 3) but that did not do the trick. The problem arises because the fractions are in this weird format.

Is there a formula or macro that I can implement to get the desired decimal results?

Thank you very much and happy Easter!


回答1:


The problem is that each of your fractions is a single unicode character. If we isolate a single character in a cell and run:

Sub WhatsIt()
    Dim v As Variant
    v = ActiveCell.Value
    MsgBox Asc(v) & vbCrLf & AscW(v) & vbCrLf & Application.WorksheetFunction.Dec2Hex(AscW(v))
End Sub

we see:

It now becomes easy to convert these items into real values. Here is a start:

Public Function Konvert(s As Variant) As Double
    Dim d As Double

    d = CDbl(Mid(s, 1, Len(s) - 1))
    If AscW(Right(s, 1)) = 8541 Then d = d + 0.625
    Konvert = d
End Function

You must expand this code to include other fractions. Here are some common ones:




回答2:


So here is one solution for you. I'm assuming that the format of the number is set and that you can't separate out the fraction.

It requires a bit of manual work and will only really be workable if you have a reasonable amount of fractions that can be manually adjusted

Here goes...

Assuming raw data is in column A, create some helper columns:

  • Column B: extract the fraction, for example: =RIGHT(A2, 1)
  • Column C: use this information to extract now the whole number, for example: =SUBSTITUTE(A2, B2, "")

Create a lookup from the unusable fraction format to one that you can use in excel:

  • First column in the lookup should be the fraction in unusable format
  • Second column should be the fractions in a usable format, first format the cells in second column > Format Cells menu > Number tab > Category as Fraction > Type "Up to one digit (1/4)"
  • Manually in the corresponding cells the equivalent values (for example, below in cell H2 write 5/8)

Now lookup the adjusted fraction in the main table

  • Column D: Lookup the adjusted fraction, for example: =VLOOKUP(B2, $G$2:$H$4, 2, 0)
  • Column E: Recalculate the usable whole number, for example: =C2 +D2

Full solution screen shot below:



来源:https://stackoverflow.com/questions/36245894/how-can-i-convert-numbers-with-subscript-fractions-to-decimal-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!