为VBA的数千个单元格添加后缀



使用VBA向数千个单元格(500000个单元格(添加后缀(在本例中仅为"ID"(的有效方法是什么?

VBA中简单而常见的"for循环"花费的时间太长。(例如For Each Cell In Selection.Cells等对于小区的数量而言需要数十分钟(。

我在下面写了这篇文章(基于互联网搜索(,使用Excel 365,但不使用Excel 2016。相反,在2016年,它取左上角的单元格,并将其粘贴到所有选定的单元格中。对如何纠正这种情况有什么想法吗?非常感谢。

Sub AddTextToEndOfCellValue()
Dim Suffix As String
Suffix = "ID"
Selection = Evaluate(Replace("REPLACE(@,LEN(@)+1,0,""" & Suffix & """)", "@", Selection.Address))
Selection.Replace Suffix, "", xlWhole, , , , False, False
End Sub

输入和预期输出样本

我只能在Excel 365上测试它,但我确信我在早期版本中成功地使用了类似的代码。

这将为所有选定的单元格添加后缀。

Sub AddTextToEndOfCellValue()
Dim Suffix As String
Suffix = "ID"
With Selection
.Value = Evaluate(.Address & "&""" & Suffix & """")
End With
End Sub

这将根据您发布的前/后图像,将后缀添加到其中有值的选定单元格中。

Sub AddTextToEndOfCellValue()
Dim Suffix As String
Suffix = "ID"
With Selection
.Value = Evaluate("IF(" & .Address & "="""", """"," & .Address & "&""" & Suffix & """)")
End With
End Sub

假设范围为"A2:D5〃;

Sub AddSuffixinSelectedRange()
Dim rng As Range, newVal
Set rng = Selection
newVal = Evaluate("A2:D5&" & """_ID""")
rng = newVal
End Sub

这将替换现有的值。。所以最好先在样本/副本上试试。

最新更新