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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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
.. 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
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
.. 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
Lots of choice. :)
Thanks for the feedback.
 
Upvote 0
Peter_SSs,

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

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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