Prepend and Append: add text to left or right of cell contents.

darro

Board Regular
Joined
Mar 10, 2009
Messages
208
I have this code (below) which does what I want, but is clearly limited in utility. How do I go about altering it so the new text can be added to any cells anywhere. At the moment it operates only on column D.

I'm not sure how to define any and all cells in vba yet?

There may even be a simpler method that I have missed. Hope you can help, there are a lot of cells waiting for me to add text to, I'm not looking forward to it if I have to do it manually.

(the cells are not all in same column and not all of one column needs to be amended hence my need for more flexibility)

Thanks for your help.

Code:
Sub AddText()
    Dim cell As Range
    Dim moretext As String
    Dim thisrng As Range
On Error GoTo endit
    whichside = InputBox("Add text to Left or right?")
    moretext = InputBox("Enter the text you want to add to cell")
Set thisrng = Intersect(ActiveSheet.Range("D:D"), ActiveSheet.Cells.SpecialCells(xlConstants))
    If whichside = "Left" Then
        For Each cell In thisrng
        cell.Value = moretext & cell.Value
    Next
    Else
        For Each cell In thisrng
        cell.Value = cell.Value & moretext
    Next
    End If
Exit Sub
endit:
    MsgBox "murrrggghhh"
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Actually I have done it with this code:

Code:
Sub AddNewText()
    Dim cell As Range
    Dim moretext As String
    Dim thisrng As Range
On Error GoTo endit
    whichside = InputBox("Add text to Left or right?")
    moretext = InputBox("Enter the text you want to add to cell")
Set thisrng = Intersect(Selection, ActiveSheet.UsedRange)
    If whichside = "Left" Then
        For Each cell In thisrng
        cell.Value = moretext & cell.Value
    Next
    Else
        For Each cell In thisrng
        cell.Value = cell.Value & moretext
    Next
    End If
Exit Sub
endit:
    MsgBox "murrrggghhh"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

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