有没有一种方法可以排除同一单元格中的部分数据?

时间:2019-06-24 17:44:20

标签: vba

我目前正在尝试编写一个代码,该代码将使用一列单元格并通过排除该列中的某些名称来对其进行清理,但困难在于我要排除的名称每次都在不同的位置

我一直在尝试找到一种使用replace函数执行此操作的方法,但是我找不到一种同时消除多个名称的好方法,并且我一直在研究mid函数由于职位的变化,我找不到使用它的方法。

例如,如果有三个单元格 1.“马特,丹尼尔,瑞安” 2.“瑞恩,马特” 3.“丹尼尔”

我想从数据中删掉“哑光”和“瑞恩”。

1 个答案:

答案 0 :(得分:0)

请参阅评论以了解更多详细信息,希望这对您有所帮助:

Sub clearNames()

Dim ws As Worksheet: Set ws = ActiveWorkbook.Sheets("Sheet1") 'declare and allocate your sheet to a variable

Dim lRow As Long: lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row 'get the last row at column 1 ("A")

Dim arrData As Variant: arrData = ws.Range("A1:A" & lRow) 'declare and populate an array with the data

Dim arrExcl() As String: arrExcl = Split("matt,ryan", ",") 'declare and populate an array with strings to exclude


Dim R As Long, X As Long
For R = LBound(arrData) + 1 To UBound(arrData) 'for each row in data array
    For X = LBound(arrExcl) To UBound(arrExcl) 'for each row in the exclusion array
        If InStr(arrData(R, 1), arrExcl(X)) > 0 Then 'if the string is found
            arrData(R, 1) = Replace(arrData(R, 1), arrExcl(X), "") 'replace it with nothing
        End If
    Next X
Next R

ws.Range("B1:B" & lRow) = arrData 'put the data back on the sheet

End Sub

可能不是您想要的,但是应该给您一个起点。

enter image description here