简体   繁体   中英

EXCEL VBA Formula - Pad a cell

I am attempting to pad an XLS cell via VBA & XLS Formula but I am stumbling:

Example: I want to append " - Test" to the contents of Cell AC2 via a formula, then paste that formula to all the remaining rows in Col AC:

lastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
strTest = " - Test"

'append

Range("AC2:AC2").Formula = "=AC2.value & value(strTest)"

'then cut and paste

Range("AC2").Select
Selection.Copy
Range("AC2:A" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

I am guessing I need to adjust the syntax of the formula to append. I've tried several variants without success.

Any suggestions?

Note: the following formula works, but only in the first row. Copy & Paste simply copies the VALUE from X2 into subsequent rows, not the formula:

 Range("X2:X2").Formula = Range("AC2:AC2").Value & strTest
 Range("X2").Select
 Selection.Copy
 Range("X2:X" & lastRow).Select
 Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
     SkipBlanks:=False, Transpose:=False

To append strTest to the cell values, you can use:

With Range("X2:X" & lastRow)
    .Value2 = .Worksheet.Evaluate("Index(" & .Address & "&""" & strTest & """,)")
End With

I think instead of this:

Range("AC2:AC2").Formula = "=AC2.value & value(strTest)"

You want this:

Range("AC2:AC2").Formula = "=AC2.value & value(" & strTest & ")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM