技术问询:提取列A字符串中的上标数字1、2并移至同行列C
提取Excel A列中的上标数字1/2并拆分到C列
方法1:VBA宏(最省心的批量处理方式)
上标属于单元格内的字符格式,普通公式没法直接识别,用VBA能精准遍历每个字符的格式属性:
- 按
Alt + F11打开VBA编辑器 - 右键左侧工程窗口,选择「插入」→「模块」
- 粘贴以下代码:
Sub ExtractSuperscriptNumbers() Dim ws As Worksheet Dim rng As Range, cell As Range Dim i As Integer, supNum As String Dim originalText As String, newText As String Set ws = ActiveSheet ' 需指定工作表可改为 Sheet1 这类名称 Set rng = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row) ' 自动抓取A列所有非空行 For Each cell In rng originalText = cell.Value newText = "" supNum = "" If originalText <> "" Then For i = 1 To Len(originalText) ' 仅识别上标格式的数字1和2 If cell.Characters(i, 1).Font.Superscript = True And _ (Mid(originalText, i, 1) = "1" Or Mid(originalText, i, 1) = "2") Then supNum = supNum & Mid(originalText, i, 1) Else newText = newText & Mid(originalText, i, 1) End If Next i End If ' 更新A列(移除上标字符),C列写入提取的数字 cell.Value = newText ws.Cells(cell.Row, "C").Value = IIf(supNum <> "", supNum, "") Next cell End Sub
- 按
F5运行宏,或回到Excel后添加按钮方便重复执行
说明:
- 自动适配A列数据范围,无需手动选择
- 仅提取上标格式的1和2,其他字符完全保留原样
- 提取到的数字按原顺序拼接在C列,无符合条件的上标则C列为空
方法2:自定义函数(适合偏好公式操作的场景)
不想用宏的话,可以创建两个自定义函数,直接在工作表中调用:
- 按
Alt + F11插入模块,粘贴以下函数代码:
Function GetSuperscript(rng As Range) As String Dim i As Integer, result As String Dim txt As String txt = rng.Value result = "" If txt <> "" Then For i = 1 To Len(txt) If rng.Characters(i, 1).Font.Superscript = True And _ (Mid(txt, i, 1) = "1" Or Mid(txt, i, 1) = "2") Then result = result & Mid(txt, i, 1) End If Next i End If GetSuperscript = result End Function Function RemoveSuperscript(rng As Range) As String Dim i As Integer, result As String Dim txt As String txt = rng.Value result = "" If txt <> "" Then For i = 1 To Len(txt) If Not (rng.Characters(i, 1).Font.Superscript = True And _ (Mid(txt, i, 1) = "1" Or Mid(txt, i, 1) = "2")) Then result = result & Mid(txt, i, 1) End If Next i End If RemoveSuperscript = result End Function
- 回到工作表,在B1单元格输入
=RemoveSuperscript(A1),下拉填充得到移除上标后的文本(如需直接覆盖A列,建议先备份原数据) - 在C1单元格输入
=GetSuperscript(A1),下拉填充即可获取提取的上标数字
注意:
- 使用自定义函数需启用宏,Excel弹出安全提示时选择「启用内容」即可
- 直接覆盖A列前务必备份数据,避免意外丢失
内容的提问来源于stack exchange,提问作者Yahooooooo
相关产品推荐
相关产品推荐

