Edit cell relative

EssKayKay

Board Regular
Joined
Jan 5, 2003
Messages
104
We have a column with numerous values in them. I want to add a variable or character to each cell. I've tried to do this by recoring a macro both absolute and relative to no avail. An example would be where I want to add "D-" in front of the data in each cell. When I use the "Record Macro" option, it replaces the data in each cell with the data in the cell I used to create the macro.

Before After

123 D-123
456 D-123
789 D-123

What I want is
D-123
D-456
D-789

Thanks,
SKK
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here's one way, at least. This will add the "D-" before the value of all cells in column A:

Code:
Sub test()
Dim cell As Range, myRng As Range

Set myRng = Range("A1", Range("A65536").End(xlUp))

For Each cell In myRng
    cell.Value = "D-" & cell.Value
Next cell

End Sub

There is also a built-in function in the ASAP Utilities addin that does this (inserts text at the beginning of all selected cells).
 
Upvote 0
Simply highlight the cells you wish to apply this to, and run the following macro:
Code:
Sub MyMacro()

    Dim cell As Range
    For Each cell In Selection
        If Len(cell) > 0 Then cell = "D-" & cell
    Next
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,203
Messages
6,054,117
Members
444,703
Latest member
pinkyar23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top