Prefix by VBA

ElNaheep

New Member
Joined
Jul 22, 2014
Messages
17
I need an Excel VBA code to add a prefix in each cell of column A when ever i enter a text to Column A.

Thank you
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Right click the sheet tab, select View Code and paste in

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Const prefix As String = "xyz"
If Target.Column = 1 Then
    Application.EnableEvents = False
    Target.Value = prefix & Target.Value
    Application.EnableEvents = True
End If
End Sub

Change the prefix to suit.
 
Upvote 0
I need an Excel VBA code to add a prefix in each cell of column A when ever i enter a text to Column A.
Does the value in the cell have to physically change so that its value permanently has the prefix added to it or would simply formatting the cell so it just looked like the prefix was added be enough (with the actual value in the cell remaining exactly as entere)?
 
Upvote 0
Thanks for fast reply

But the problem it add this prefix each time i activate the cursor in the cell!!
how to fix it? so it only add the prefix once in a cell and if i decideded to delete it from a cell it don't add it again to that cell ?

Thank you
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Const prefix As String = "xyz"
If Target.Column = 1 Then
    Application.EnableEvents = False
    If Target.Value <> "" Then Target.Value = prefix & Target.Value
    Application.EnableEvents = True
End If
End Sub

This will only activate when you change a cell, not when you select it.
 
Upvote 0
I want it to add the prefix once to the cell and if decided to make a change to the cell, it will not add the prefix again
 
Upvote 0
It would have helped if you had posted your complete question in the first place and not a simplification. Maybe

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Const prefix As String = "xyz"
If Target.Column = 1 Then
    Application.EnableEvents = False
    If Target.Value <> "" And Left(Target, Len(prefix)) <> prefix Then Target.Value = prefix & Target.Value
    Application.EnableEvents = True
End If
End Sub

but I think that Rick's idea is the best.
 
Upvote 0
I'm soo sorry to bother you sir, and you are right I've should explain what i want from the start.

And now i want another thing, Can i make excel notify me immediately for duplication which occur in column A ?

thank you
 
Upvote 0

Forum statistics

Threads
1,222,043
Messages
6,163,565
Members
451,844
Latest member
ddnndd1234

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