adding comma to string value in Range

ilcaa

Well-known Member
Joined
May 25, 2005
Messages
751
Office Version
  1. 365
Platform
  1. Windows
i have 6000 rows in my range, i want to add a comma to the 7 character location from end.

i have this formula that i tested in a single cell and it works BUT when I try to apply it to the entire range using a With Range, the syntax is off (get mismatch error) can anyone assist with proper syntax

the formula is. Take the left most characters - last 6, then add a comma, then add back the last 6 characters.

Code:
set Rng = range("a1:a6000"

With Rng
        .Value = Left(.Value, Len(Rng) - 6) & "," & Right(Rng, 6)
End With
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You cannot change multiple values like that, try
Code:
Sub ilcaa()
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(len(@)<6, @,replace(@,len(@)-5,0,"",""))", "@", .Address))
   End With
End Sub
 
Upvote 0
You cannot change multiple values like that, try
Code:
Sub ilcaa()
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(len(@)<6, @,replace(@,len(@)-5,0,"",""))", "@", .Address))
   End With
End Sub

hi fluff

i need to change the hard coded value of the 6th space from end to a variable.

i am struggling to write the proper syntax with all those quotes, etc. info on EVALUATE is very limited with only basic examples on www.

how can i replace the hard-coded len(@)-5 with a variable name?

thanks alot. hope i made sense
 
Upvote 0
How about
Code:
Sub ilcaa()
   Dim Lngth As Long
   Lngth = 6
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(len(@)< " & Lngth & ", @,replace(@,len(@)-" & Lngth - 1 & ",0,"",""))", "@", .Address))
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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