在Excel 2011中美化科学记数法

Excel默认显示如“1.00E + 03”的科学记数法。 有没有办法让它更吸引眼球? 我在想这样的事情:

在这里输入图像说明

这应该是可能的Unicode字符,但我没有以前的VBA的经验。 任何build议,欢迎。

如果你有像这样的数据:

在这里输入图像描述

select单元格并运行这个macros:

Option Explicit Sub dural() Dim t As String, np As String, midl As String Dim signp As String, i As Long, ary, expp As String Dim msg As String, neg As Boolean, r As Range midl = " x 10" ary = Split("8304,185,178,179,8308,8309,8310,8311,8312,8313", ",") For Each r In Selection msg = "" t = r.Text If Left(t, 1) = "-" Then neg = True t = Mid(t, 2) Else neg = False End If np = Mid(t, 1, 4) signp = Mid(t, 6, 1) If signp = "+" Then signp = ChrW(8314) Else signp = ChrW(8315) End If expp = Mid(t, 7) If Left(expp, 1) = "0" Then expp = Mid(expp, 2) For i = 1 To Len(expp) msg = msg & ChrW(ary(CLng(Mid(expp, i, 1)))) Next i msg = np & midl & signp & msg If neg Then msg = "-" & msg msg = Chr(34) & msg & Chr(34) r.NumberFormat = msg & ";" & msg & ";" & msg & ";" Next r End Sub 

生产:

在这里输入图像描述

注意:

  1. 只有格式已经改变,而不是基础的价值
  2. 假定单元格有常量,而不是公式
  3. 字体支持Unicode

这个过程将起作用(它将指数的数字变成上标,所以应该适用于任何字体):

 Option Explicit Sub SciIt(ModCell As String) Dim cl As Range Dim Form As String Set cl = Range(ModCell) If IsNumeric(cl.Value) Then Form = Format(cl.Value, "0.00E+0") cl.Value = Left(Form, InStr(Form, "E") - 1) & " x 10" & Mid(Form, InStr(Form, "E") + 1) cl.Characters(Start:=InStr(cl.Value, "x") + 4).Font.Superscript = True End If End Sub 

请注意,这会破坏之前存在的值,所以如果您需要使用公式的数字,请将副本保留在其他地方。 它也将失去与前一个单元格的链接,因此如果原始值发生更改,则必须重新input公式,然后重新运行。

我想出了一个完美的工作的Scala脚本:

 import java.awt.Toolkit import java.awt.datatransfer.{StringSelection, DataFlavor} /** * Created by IDEA on 13/10/15. */ object Scientific { val superscriptMap = { var tmp = collection.mutable.Map( '0' -> '\u2070', '1' -> '\u00b9', '2' -> '\u00b2', '3' -> '\u00b3' ) val x = ('4' to '9').zip('\u2074' to '\u2079').toList for ((k, v) <- x) { tmp.update(k, v) } tmp.toMap } type ExponentTuple = (Int, String) def dealWithSign(s: String): ExponentTuple = { if (s.head == '+') { (1, s.tail.dropWhile(_ == '0')) } else if (s.head == '-') { (-1, s.tail.dropWhile(_ == '0')) } else { (1, s) } } def snBeautify(s: String): String = { val s1 = s.split("[Ee]").take(2) require(s1.length == 2) val coefficient = s1(0) val (sign, exponent) = dealWithSign(s1(1)) val part1 = coefficient + '\u00d7' + "10" val part2 = exponent.map(superscriptMap(_)) // if the number is negative, provide a superscript minus if (sign < 0) part1 + '\u207b' + part2 else part1 + part2 } def snBeautify1(s: String): String = { s.split("\\s").map(snBeautify(_)).mkString("\n") } def snBeautify(printOut: Boolean = true): Unit = { val toolkit = Toolkit.getDefaultToolkit val clipboard = toolkit.getSystemClipboard val s1 = clipboard.getData(DataFlavor.stringFlavor).asInstanceOf[String] val s2 = snBeautify1(s1) val s2Select = new StringSelection(s2) if (printOut) println(s2) clipboard.setContents(s2Select, s2Select) } } object Test1 { // val s = List("4.2E+5", "4.2e5", "4.2e-5", "4.2E-5", "-4.2e-5") // s.map(snBeautify(_)) // snBeautify1(s.mkString("\n")) // val s1 = "1.74E-11\n2.72E-12\n2.20E-11\n2.20E-11\n2.20E-11\n2.20E-11\n2.20E-11\n2.20E-11\n2.20E-11\n2.20E-11\n2.20E-11\n6.42E-12\n7.13E-11\n4.02E-11\n7.84E-12\n5.87E-12\n1.55E-11\n1.07E-10\n1.04E-10\n3.81E-11\n4.46E-11\n4.46E-11\n4.46E-11\n4.46E-11\n1.23E-10\n2.45E-11\n8.24E-12" // snBeautify1(s1) // Copy the cells you want to reformat first Scientific.snBeautify() } 

不是原生的Excel解决scheme,但至lessScala比VBA好得多。

您可以复制要重新格式化的单元格,然后运行snBeautify函数,重新格式化的数字现在位于剪贴板中,准备好粘贴到Excel或Word中。

这个解决scheme的一个优点是它同样适用于LibreOffice或OpenOffice。

您还可以轻松地将其修改为shell脚本并将其绑定到某个键盘快捷键。