Inserting a text as a "prefix" to multiple cells with different values

fosiza

New Member
Joined
Sep 1, 2014
Messages
11
Helo good people of mrexcel forum,

Lets say I have table like this
IDItems
1Orange
1Bread
2Milk
2Bread
2Apple
3Knife
3Plier

<tbody>
</tbody>

I want to add a "prefix" (dont know if I use the correct term here) to every of the ID numbers. Lets say I want to add prefix "10". So the transformed table would look like this:

IDItems
101Orange
101Bread
102Milk
102Bread
102Apple
103Knife
103Plier

<tbody>
</tbody>

How can I do this?

EDIT: I mean of course, aside from manually typing "10" to every cell lol
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub test()
Dim LR As Long, i As Long
Dim prefix As String
prefix = InputBox("Enter prefix")
If prefix = "" Then Exit Sub
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("A" & i)
        .Value = prefix & .Value
    End With
Next i
End Sub
 
Upvote 0

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
59,294
Office Version
  1. 365
Platform
  1. Windows
.. or applying to the whole range at once and skipping any empty cells
Rich (BB code):
Sub AddPrefix()
  Dim sAddr As String
  
  sAddr = "A2:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(sAddr).Value = Evaluate("IF(LEN(" & sAddr & "),10 & " & sAddr & ","""")")
End Sub
 
Upvote 0

fosiza

New Member
Joined
Sep 1, 2014
Messages
11
Try

Code:
Sub test()
Dim LR As Long, i As Long
Dim prefix As String
prefix = InputBox("Enter prefix")
If prefix = "" Then Exit Sub
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("A" & i)
        .Value = prefix & .Value
    End With
Next i
End Sub

Brilliant! Thank you my good man!

in a spare column you could have ="10"&A2 (assuming ID column is A), copy this down and then pastespecial

="10"&cell reference

Thanks for the alternatives guys!
 
Upvote 0

fosiza

New Member
Joined
Sep 1, 2014
Messages
11
.. or applying to the whole range at once and skipping any empty cells
Rich (BB code):
Sub AddPrefix()
  Dim sAddr As String
  
  sAddr = "A2:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(sAddr).Value = Evaluate("IF(LEN(" & sAddr & "),10 & " & sAddr & ","""")")
End Sub

Also a brilliant solution! Thank you Peter.

Alright guys, I think Ive learned enough today, thank you all for the solutions.
 
Upvote 0

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
59,294
Office Version
  1. 365
Platform
  1. Windows
Lots of choice. :)
Thanks for the feedback.
 
Upvote 0

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
Peter_SSs,

Another Evaluate solution to add to my archives - thank you so much.
 
Upvote 0

Forum statistics

Threads
1,191,501
Messages
5,986,927
Members
440,067
Latest member
Swatts1

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
Top