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
[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Items[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Bread[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Milk[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Bread[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Knife[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Plier[/TD]
[/TR]
</tbody>[/TABLE]

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:

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Items[/TD]
[/TR]
[TR]
[TD]101[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]101[/TD]
[TD]Bread[/TD]
[/TR]
[TR]
[TD]102[/TD]
[TD]Milk[/TD]
[/TR]
[TR]
[TD]102[/TD]
[TD]Bread[/TD]
[/TR]
[TR]
[TD]102[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]103[/TD]
[TD]Knife[/TD]
[/TR]
[TR]
[TD]103[/TD]
[TD]Plier[/TD]
[/TR]
</tbody>[/TABLE]

How can I do this?

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

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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

Forum statistics

Threads
1,222,175
Messages
6,164,398
Members
451,890
Latest member
JamieS

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