简体   繁体   English

如何替换列中单元格中的文本?

[英]How can I replace the text in cells in a column?

I'm building a tool to parse/format copy pasted data into a more organized table.我正在构建一个工具来解析/格式化复制粘贴的数据到更有条理的表格中。

The final table is 4 columns and the 3rd column is email addresses.最终表有 4 列,第 3 列是电子邮件地址。

I would like to replace (@gmail.com) in the email address column with a value that says "external email address".我想将电子邮件地址列中的 (@gmail.com) 替换为“外部电子邮件地址”的值。 The size of the table changes when new data in input.输入新数据时表的大小会发生变化。

With my limited knowledge I've not been successful.以我有限的知识,我还没有成功。

Dim lastRow as long, ws as Worksheet, i as long

Set ws = ThisWorkBook.Worksheets("YourWorksheetName")

' Suppose your third column is column C
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
' Suppose the first row is the header, column C = 3 in terms of index
For i = 2 To lastRow
    If ws.Cells(i,3).Value Like "*@gmail.com*" Then
         ws.Cells(i,3).Value = "external email address"
    End If
Next i

It'd be nice if You showed what You mean by "A lot of the stuff is already done" and what You've achieved so far but...如果你能展示你所说的“很多事情已经完成”是什么意思,以及你到目前为止所取得的成就,那就太好了,但是......

You could try using InStr() like this,你可以尝试像这样使用InStr()

Sub ReplaceExternalEmails()

    'ofc sheet's and listObj's indexes or "names" matters and depends on your case, try to figure these out
    Dim someTable As ListObject
    Set someTable = Sheets(1).ListObjects(1)

    'Loop through 3rd column to check for external mails
    Dim cel As Range
    For Each cel In someTable.DataBodyRange.Columns(3).Cells
        If InStr(cel.Value, "@gmail.com") Then
            cel.Value = "external email address"
        End If
    Next cel

End Sub

You could also add您还可以添加

cel.ClearFormats

inside for loop, or some other formating so it won't stay underlined blue if You care somehow.在 for 循环或其他一些格式中,如果您在意的话,它不会保持蓝色下划线。

Another way to approach this (which is probably faster with larger datasets) is by replacing values, which is equivalent to CTRL+F / Replace method like this,解决此问题的另一种方法(对于较大的数据集可能更快)是替换值,这相当于CTRL+F / Replace方法,如下所示,

Sub ReplaceExternalEmails()

    Dim someTable As ListObject
    Set someTable = Sheets(1).ListObjects(1)

    Dim fnd As Variant: fnd = "*@gmail.com"
    Dim rpl As Variant: rpl = "external email address"

    someTable.DataBodyRange.Columns(3).Replace _
        What:=fnd, Replacement:=rpl

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一系列单元格转换为文本? - How can I convert a range of cells to text? 如何使用EXCEL公式从一列单元格中提取具有特定模式的文本? - How can I extract a text that has a specific pattern from a column of cells using EXCEL formula? 如何从列中的所有单元格中删除第二个句号后的所有文本? - How can I remove all text after the 2nd full-stop from all cells in a column? 如何覆盖数据透视表(设置单元格)上的列标签文本? - How can I override the column label text on a Pivot Table (Aspose Cells)? 如何在VBA中突出显示列的单元格中合并多个条件? - How can I incorporate multiple conditions in highlighting cells of a column in VBA? 如何将选定的单元格(列)格式化为9位数字 - How can I format selected cells (column) to 9 digit number 如何包含方括号的列中所有单元格的内容? - How can I embrace the content of all cells in a column with brackets? 如何从具有图像的列中删除空白单元格? - How can I remove blank cells from a column having images? 如何检查 Excel 中的值是否为 0? (如果同一列中有空白单元格) - How can I check if a value is 0 in Excel? (If there are blank cells in the same column) 如何删除整列单元格中空格后的内容? - How can I delete anything after the space of in the cells of a whole column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM