簡體   English   中英

僅向活動合並單元格添加一次字符串 - Excel VBA 宏

[英]Add string to active merged cells only once - Excel VBA macro

我有一列帶有文本的各種水平合並單元格(3),我想在每個選定的單元格之前添加一個字符串,但只添加一次(作為一個合並單元格)。

我有這段代碼,但它添加了三次字符串 - 計算合並單元格中的每個單元格:

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 ). 直接使用c你的問題就解決了。 寫入作為合並單元格一部分的單元格而不是合並區域的第一個單元格將被簡單地忽略。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM