我有一张长表,其中列有样品、分析编号、测试方法等,以及重要的分析化学元素。我想先按样品对表格进行排序,然后按化学元素进行排序。元素符号应按原子序数递增的顺序排列:H < He < Li < Be < B < C < N 等,一直到 Pb。这可以在 Excel 中完成吗?
答案1
按原子序数对数据进行排序。以下是从 Wikipedia 返回一些数据的查询:
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)排序
选择所有数据。
菜单数据 > 排序...
首先按样本排序,然后按原子序数排序。