Adding Text to existing text string macro

kstate1988

New Member
Joined
Jun 7, 2016
Messages
9
What I need, is a macro that can look in Column D and add “/” and “R” to the existing 7 character text string.

Cell (2D) has what the end result should be for all cells in column D, once the macro runs.
Matched
235/65R16
2757018
2256016
2356016
2457017
2256016

<tbody>
[TD="colspan: 2"]Dealer Description
[/TD]

</tbody>


This is what I have at this time.

Sub arranges()
Dim str As String
str = ActiveCell.Value
ActiveCell.Value = Left(str, 3) & "/" & Mid(str, 4, 2) & "R" & Mid(str, 6, 2) _
End Sub

Any help you can provide would be greatly appreciated.
 

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
Try

Code:
Sub arranges()
Dim str As String, c As Range, lr As Long
lr = Cells(Rows.Count, "D").End(xlUp).Row
For Each c In Range("D2:D" & lr)
   str = c.Value
   c.Value = Left(str, 3) & "/" & Mid(str, 4, 2) & "R" & Mid(str, 6, 2)
Next c
End Sub
 
Upvote 0
Here is another macro using no loops that will also do what was asked for...
Code:
[table="width: 500"]
[tr]
	[td]Sub AddSlashAndR()
  Dim Addr As String
  Addr = "D2:D" & Cells(Rows.Count, "D").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF(@="""","""",REPLACE(REPLACE(@,6,0,""R""),4,0,""/""))", "@", Addr))
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
If you just need it displayed that way you could also use a custom number format of:
000\/00R00
I cannot believe I didn't think of that for my macro. Here is my macro revised to use the TEXT function to implement that format pattern...
Code:
[table="width: 500"]
[tr]
	[td]Sub AddSlashAndR()
  Dim Addr As String
  Addr = "D2:D" & Cells(Rows.Count, "D").End(xlUp).Row
  Range(Addr) = Evaluate("TEXT(" & Addr & ",""000\/00R00"")")
End Sub[/td]
[/tr]
[/table]

Note: Make sure to read Scott's suggestion in Message #5 in case that solution will serve your needs.
 
Last edited:
Upvote 0
Thanks everyone for the fast response and the excellent suggestions!

These answers solved my question.

Nicely done.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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