如何一次删除多个单元格中的特定文本?


1

我有大量的电子邮件地址;但为了简单起见,我正在处理3到10个电子邮件地址,这些地址在1个单元格中输入,我似乎无法做到 MASS 删除特定的。有什么办法可以从单元格中删除特定的电子邮件地址, GIVEN 我有一个我想删除的特定电子邮件地址列表。这是找到重复和删除的问题还是更多吗?

详细说明::

在A2中我有 abc@abc.com aaa@abc.com bbb@abc.com ccc@abc.com

在单元格A3中我有 xyz@xyz.com xxx@xyz.com yyy@xyz.com zzz@xyz.com

在单元格H1到H5中 我有abc@abc.com xxx@xyz.com bbb@abc.com ccc@abc.com yyy@xyz.com(每个电子邮件地址都在单个单元格中)

有什么方法可以删除A2和A3中的特定电子邮件地址,以便我只留下aaa @ abc.com,xyz @ xyz.com和zzz@xyz.com?

附加信息:我正在使用Excel for Mac 2011

任何帮助将受到高度赞赏。

谢谢!

Answers:


0

做你的后续问题 - 我可以更快地运行此宏吗? - 我在这里提交我的答案并投票将该问题作为副本结束。


如果我理解你,你想要获取H列中的所有值并从E列中删除它们?我会用一些阵列来加速它 -

Option Explicit
Sub DoTheThing()
Application.ScreenUpdating = False
Dim lastrow As Integer
'Find last row in column H to size our array
lastrow = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).row

'Declare the array and then resize it to fit column H
Dim varkeep() As Variant
ReDim varkeep(lastrow - 1)

'Load column H into the array
Dim i As Integer
For i = 0 To lastrow - 1
    varkeep(i) = Range("H" & i + 1)
Next

Dim member As Variant
'find last row in column E
lastrow = ActiveSheet.Cells(Rows.Count, "E").End(xlUp).row

'loop each cell in column E starting in row 2 ending in lastrow
For i = 2 To lastrow
    'Make a new array
    Dim myArray As Variant
    'Load the cell into the array
    myArray = Split(Cells(i, 5), " ")
    Dim k As Integer
    'for each member of this array
    For k = LBound(myArray) To UBound(myArray)
        member = myArray(k)
        'call the contains function to check if the member exists in column H
        If Contains(varkeep, member) Then
            'if it does, set it to nothing
            myArray(k) = vbNullString
        End If
    Next
    'let's reprint the array to the cell before moving on to the next cell in column E
    Cells(i, 5) = Trim(Join(myArray, " "))
Next
Application.ScreenUpdating = True
End Sub


Function Contains(arr As Variant, m As Variant) As Boolean
    Dim tf As Boolean
    'Start as false
    tf = False
    Dim j As Integer
        'Search for the member in the keeparray
        For j = LBound(arr) To UBound(arr)
            If arr(j) = m Then
                'if it's found, TRUE
                tf = True
                Exit For
            End If
        Next j
        'Return the function as true or false for the if statement
        Contains = tf
End Function

这将从列H中创建一个数组。然后它遍历E列中的每个单元格,将其解析为数组,针对保持数组搜索该数组的每个成员,如果找到,则删除该数组的成员。通过单元格后,它重新打印数组,找不到找到的数组。


数组通常比逐项更快,但另外,我们创建自己的函数而不是使用 Find and Replace 方法。唯一的问题是数据中可能有额外的空格。如果是这样,我们可以快速查找并替换它。我发现更容易将数组的成员设置为空,而不是重新调整数组大小并移动元素。


为了完整起见,这里是一个从E列中删除多余空格的例程

Sub ConsecSpace()
Dim c As Range
Dim lastrow As Integer
lastrow = ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Row
Dim strValue As String
For Each c In Range("E2:E" & lastrow)
    strValue = c.Value
    Do While InStr(1, strValue, "  ")
        strValue = Replace(strValue, "  ", " ")
    Loop
    c = strValue
Next
End Sub

非常感谢。这段代码运行得更快,并且很快就完成了。我相信你曾经帮我解决过以前的问题,真的不能感谢你,其他人帮我解决了这个问题。
Jase

1

除非我误解,你在这里做的最好的事情就是使用 查找和替换

找到您想要的电子邮件,并将其替换为空值。

这可能会对您的H值产生负面影响,但如果您手动找到并替换(一次一个),它应该简单快速。

VBa解决方案 - 首先备份。

根据你的例子,我假设你的查找从H1开始并在某个时刻结束。

我还假设其他数据从A2开始并在A中结束一些行

Option Explicit
Sub DoTheThing()
 Dim keepValueCol As String
 keepValueCol = "H"               'You may need to update this

 Dim row As Integer
 row = 2                          'what row do the values start in column A

 Dim keepValueRow As Integer
 keepValueRow = 1

 Do While (Range("A" & row).Value <> "")

    Do While (Range(keepValueCol & keepValueRow).Value <> "")

    Range("A" & row).Value = Replace(Range("A" & row).Value, Range(keepValueCol & keepValueRow).Value, "")
    Range("A" & row).Value = Trim(Replace(Range("A" & row).Value, "  ", " "))

    keepValueRow = keepValueRow + 1
    Loop


 keepValueRow = 1
 row = row + 1
 Loop

End Sub

如果我按照建议的路线找到并逐一替换,我需要花费很多年的时间。我正在处理超过2000个需要在单元格内删除的特定电子邮件地址。我正在寻找一种更快的方法来做到这一点。但我感谢你的帮助!
Jase

如果您拥有真正的Excel版本,我可以提供帮助,因为它可以很容易地在VBa中完成!对不起@Jase
Dave

Office for Mac确实有一个开发人员选项,其中包括宏和Visual Basic编辑器。有没有办法帮助你实现这两项功能?
Jase

如果我的问题很容易解决,我也可以在PC上找到我的手。 @戴夫
Jase

@Jase,更新,看看是否有帮助
Dave

0

循环浏览要删除的电子邮件列表,只需选择范围并将其替换为空字符串,如下所示:

Sub Replace_Email_List()

Dim rList as Range, rReplace as Range
Dim sEMail as String

'' Following your Example
Set rList = Range("H1",Range("H1").End(xlDown)) '' or just Range("H1:H5")
Set rReplace = Range("A1",Range("A1").End(xlDown)) '' or just Range("A1:A2")

For i = 1 to rList.Row
    sEMail = rList(i,1)

    rReplace.Replace What:=sEmail, Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True
Next i

End Sub

希望我的回答可以帮到你

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.