从整个列 excel-VBA 中删除重复出现的子字符串

Remove a recurring substring from an entire column excel-VBA

我有一个名为 "Name" 的列,其条目有

在我需要的行条目部分之前。例如

"Name: {white space} ABC Retailers" 而不是 "ABC Retailers"

"ame: {white space} XYZ Suppliers" 而不是 "XYZ Suppliers"

我只想去掉这些行条目中的 "Name:" 和 "ame:",然后还有左边的空格。

我该怎么做?

我尝试使用 Replace,开始时是这样的:

Sub fix_names()
    'declare variables
    Dim arr() As Variant
    arr = Range("B2:B3781")

    'count rows
    noRows = Range("B2:b3781").Rows.Count

    'begin clean up operation
    For i = 1 To noRows
        arr(i) = (Replace(arr(i), "Name:", ""))
    Next i

End Sub

但无济于事。我究竟做错了什么?我该如何解决这个问题?

根据我的评论,一个简单的公式就可以做到:

=TRIM(MID(B2,SEARCH(":",B2) +1, LEN(B2)))

如果你想在 vba 中使用它,那么:

Sub fix_names()
'declare variables
Dim i as long
Dim arr() As Variant
arr = Range("B2:B3781")

'begin clean up operation
For i = LBound(arr, 1) To UBound(arr, 1)
    arr(i, 1) = Trim(Mid(arr(i, 1), WorksheetFunction.IfError(Application.Find(":", arr(i, 1)), 1) + 1))
Next i
Range("B2:B3781").Value = arr
End Sub

问题是,即使您加载的数组只有一列,它仍然是一个二维数组,必须这样对待。