在Excel 2011中美化科学记数法

时间:2015-10-13 14:35:13

标签: excel vba

Excel默认显示类似“1.00E + 03”的科学记数法。有没有办法让它对眼睛更有吸引力?我在考虑这样的事情:

PHP str_replace

这应该可以使用unicode字符,但我没有以前的VBA经验。欢迎任何建议。

3 个答案:

答案 0 :(得分:3)

如果您有以下数据:

enter image description here

选择单元格并运行此宏:

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

生产:

enter image description here

<强> 注:

  1. 仅格式已更改,而不是基础值
  2. 假设单元格具有常量,而不是公式
  3. font支持unicode

答案 1 :(得分:0)

此过程将起作用(它将指数的数字转换为上标,因此应适用于任何字体):

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

请注意,这会破坏之前的值,因此如果您需要使用公式的数字,请在其他位置保留副本。它也将失去前一个单元格的链接,因此如果原始值发生变化,则必须重新输入公式,然后再次运行。

答案 2 :(得分:0)

我已经提出了一个可以很好地完成工作的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解决方案,但至少Scala比VBA好得多。

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

此解决方案的一个优点是它同样适用于LibreOffice或OpenOffice。

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

相关问题