我有一個長表格,其中列有樣品、分析編號、測試方法等,重要的是,還包括分析的化學元素。我想先按樣品對表格進行排序,然後按化學元素進行排序。元素符號應依原子序遞增的順序排列:H < He < Li < Be < B < C < N 等,一直到 Pb。這可以在 Excel 中完成嗎?
答案1
依原子序數對資料進行排序。以下是從維基百科傳回一些資料的查詢:
let
Source = Web.Page(Web.Contents("https://en.wikipedia.org/wiki/List_of_elements_by_atomic_properties")),
Data0 = Source{0}[Data],
#"Changed Type" = Table.TransformColumnTypes(Data0,{{"Z", type text}, {"Name", type text}, {"Symbol", type text}, {"Average atomic mass", type text}, {"Electronegativity (Pauling)", type text}, {"First Ionization Energy (eV)", type text}, {"Radii (pm) Atomic", type text}, {"Radii (pm) Van der Waals", type text}, {"Radii (pm) Covalent", type text}, {"Valence electrons", type text}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Z] <> "Z")),
#"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"Z", Int64.Type}, {"Valence electrons", Int64.Type}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type1","—","",Replacer.ReplaceText,{"Radii (pm) Van der Waals"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","—","",Replacer.ReplaceText,{"Radii (pm) Covalent"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","—","",Replacer.ReplaceText,{"Radii (pm) Atomic"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Replaced Value2",{{"Radii (pm) Atomic", Int64.Type}, {"Radii (pm) Covalent", Int64.Type}, {"First Ionization Energy (eV)", type number}}),
#"Replaced Value3" = Table.ReplaceValue(#"Changed Type2","—","",Replacer.ReplaceText,{"Electronegativity (Pauling)"}),
#"Changed Type3" = Table.TransformColumnTypes(#"Replaced Value3",{{"Electronegativity (Pauling)", type number}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type3",{{"Z", "Atomic Number"}})
in
#"Renamed Columns"
您可以使用資料>取得資料>從其他來源>空白查詢,然後當Power Query編輯器開啟時,您可以開啟進階編輯器,然後將上面的查詢貼到進階編輯器視窗中。當您按一下「關閉並載入」時,它會將資料放回工作簿中的新工作表中。然後您可以:
- 用於
XLOOKUP
將原子序數新增至資料中,然後按該列排序,或者如果您想要變得更複雜但更強大, - 將兩個資料集載入到 PowerPivot 資料模型,在元素名稱上建立聯接,然後設定元素列的排序屬性以使用原子數。如果這樣做,則從 Power Pivot 資料模型建立的所有資料透視表中的元素將按原子序數排序
順便說一句,如果將該查詢載入到 Power Query 中,您也可以選擇「名稱」列,然後使用「轉換」>「建立資料類型」選項,如下所示:
這可以讓你做一些有趣的事情,例如:
答案2
答案3
(1) 建立自訂Excel VBA函數ATOMICNUMBER()
按Alt+F11開啟 Visual Basic 編輯器(在 Mac 上,按FN+ ALT+ F11),然後按一下「插入」>「模組」。
將以下程式碼複製並貼上到新模組中。
Function ATOMICNUMBER(arg1 as String) as Integer
elementSymbols = Array("foo", "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og")
ATOMICNUMBER = 0
For i = 1 To UBound(elementSymbols)
If elementSymbols(i) = arg1 Then
ATOMICNUMBER = i
Exit For
End If
Next
End Function
關閉 Visual Basic 編輯器。
(2) 新增輔助列
在元素符號旁邊插入一個新列。
填充它=ATOMICNUMBER(
包含元素符號的相鄰單元格)
。
(3)排序
選擇所有數據。
選單資料 > 排序...
首先按樣品排序,然後按原子序數排序。