[英]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.