Processing math: 100%

highlight.pack.js

2012年5月9日水曜日

Excel で 1.20*10^3 といった表記を一般的な書式に変更するマクロ

Excelで1.20*10^3といった指数表記で記述されたセルの内容を、 1.20×103のような実験などでよく出てくる形に自動的に変換するマクロを作ってみました(と言っても参考ページのをほとんどパクってますが)。

合わせて、1.20*10^3のような表記の文字列を計算して数式にするマクロも作りました。

Sub Transform()
Dim cell As Range
For Each cell In Selection
cell = Replace(cell, "*", "×")
exps = Split(cell, "^")
If Not IsEmpty(cell) Then
If UBound(exps) = 1 And exps(0) <> "" Then
cell = exps(0) & exps(1)
cell.Characters(Len(exps(0)) + 1).Font.Superscript = True
End If
End If
Next
End Sub
Function CalcExp(cell)
Dim num As Double
exps = Split(cell, "*10^")
If Not IsEmpty(cell) Then
If UBound(exps) = 1 And exps(0) <> "" Then
num = exps(0) * 10 ^ exps(1)
End If
End If
CalcExp = num
End Function
view raw gistfile1.vb hosted with ❤ by GitHub

Transformは変換したいセルを選択して実行すればOKです。 CalcExpはCalcExp(A1)みたいな感じでセルを参照してあげて下さい。

Excel質問掲示板(VBA) Re[1

: 数字を上付き文字を含んだ指数表示にするには] を参考にさせて頂きました。

0 件のコメント:

コメントを投稿