[英]Add string to active merged cells only once - Excel VBA macro
I have a column of various horizontally merged cells (3) with text and I want to add a string before every selected one of them but only once (to a merged cells as one).我有一列带有文本的各种水平合并单元格(3),我想在每个选定的单元格之前添加一个字符串,但只添加一次(作为一个合并单元格)。
I have this code but it adds the string three times - counts every single cell in a merged one:我有这段代码,但它添加了三次字符串 - 计算合并单元格中的每个单元格:
Sub Add2Formula()
' Add text
For Each c In Selection
c.Activate
ActiveCell.FormulaR1C1 = "Text - " & ActiveCell.Formula
Next c
End Sub
Don't select
or activate
anything in VBA (read How to avoid using Select in Excel VBA ). Don't
select
or activate
anything in VBA (read How to avoid using Select in Excel VBA ). Use c
directly and your problem will be gone.直接使用
c
你的问题就解决了。 Writing into a cell that is part of a merged cell and not the first cell of the merged area will simply be ignored.写入作为合并单元格一部分的单元格而不是合并区域的第一个单元格将被简单地忽略。
Sub Add2Formula()
Dim c As Range
For Each c In Selection
c.Value = "Text - " & c.Value
Next c
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.