修剪从逗号 - Excel 开始的字符串



我有一个csv表,其中包含员工的姓氏,名字的数据。下面是一个示例。(这些值位于单个单元格中)

Flynn, Jeremy  
Early, Allison   
Epstein, David   
Newman, Joanna  
Biord, Brooke

我需要左修剪数据,以便我只需要名字,没有任何尾随空格或逗号。

输出样本应为:

Jeremy 
Allison 
David 
Joanna 
Brooke

如何编写将处理超过 5000 条记录的整个工作表的公式或宏。

公式,在一个空列中放置:

=TRIM(MID(A1,Find(",",A1)+1,LEN(A1)))

其中 A1 是列表的第一个单元格。 然后复制整个列表。

如果你想要vba,这几乎可以立即完成:

Sub firstName()
    Dim rng As Range
    With ActiveWorkbook.ActiveSheet
        'Change the Second Criterion in Each .Cells() to the column number of your data.
        Set rng = .Range(.Cells(1, 1), Cells(.Rows.Count, 1).End(xlUp))
        'Change the Offset value to the number of columns to offset the answer.
        rng.Offset(, 1).Value = .Evaluate("Index(TRIM(MID(" & rng.Address & ",Find("",""," & rng.Address & ")+1,LEN(" & rng.Address & "))),)")
    End With
End Sub

它假设您的数据在 A 列中,并将其放在 B 列中

作为使用 REPLACE 函数右侧已用列中的公式,

=REPLACE(A1, IFERROR(FIND(",", A1), LEN(A1)), LEN(A1), TEXT(,))

作为使用 Range.Replace 方法的 VBA 子过程,

Sub firstOnly_bySel()
    With Selection
        .Replace what:=",*", replacement:=vbNullString, lookat:=xlPart
        'selection.replace(
    End With
End Sub

选择一个或多个单元格,然后从"宏"对话框中运行子单元 ([alt]+[F8])

只需执行简单的查找和替换即可。找到",*"并将"替换"留空。

相关内容

最新更新