I have a spreadsheet that has nearly 400 equations in it. The creator of the spreadsheet copy/pasted the values from another source. There are several that use scientific notation in their variable coefficients. I am looking for a way to convert them into decimal notation so that I can store them in a database and execute using dynamic sql.
Here's an example of what I have
y = -6E-05x4 + 0.0272x3 - 1.4546x2 - 17.743x + 8137.3
I want it to be this
y = -0.00006x^4 + 0.0272x^3 - 1.4546x^2 - 17.743x + 8137.3
The column of equations is considered a "General" column by Excel. I tried changing the column type, hoping that Excel would recognize the function and convert the coefficients, but it won't. I am trying to not have to re-write all these equations manually converting the scientific notation. I am also open to using a 3rd party piece of software to convert the scientific notation to decimal representation.
答案 0 :(得分:1)
您可以使用VBA UDF(用户定义的函数)执行此操作。 我
大于约10 ^ 28的值将保持不变。小于约10 ^ -28的值将转换为零(0)。
Option Explicit
Function ConvertToDecimal(S As String) As String
Dim RE As Object, MC As Object, M As Object
'Regex pattern to extract Scientific numbers
Const sPat As String = "[-+]?\d+(\.\d+)?[Ee][+-]\d+"
Dim sRepl As String, I As Long
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.Pattern = sPat
If .Test(S) = True Then
Set MC = .Execute(S)
sRepl = S
For Each M In MC
On Error Resume Next
sRepl = Replace(sRepl, M, Format(CDec(M), "#,##0.0" & WorksheetFunction.Rept("#", 30)), 1, 1)
Select Case Err.Number
Case 6 'Overflow
'do nothing. sRepl is unchanged
Case Is <> 0
Debug.Print Err.Number, Err.Description
Stop 'stop to debug
End Select
On Error GoTo 0
Next M
End If
End With
ConvertToDecimal = sRepl
End Function