Detecting commas in a cell and if more than one, replace the last with a "&"

zoog25

Active Member
Joined
Nov 21, 2011
Messages
418
Hello all,

I have a program i'm writing in which a word document is filled in by content from specific cells. For a special case, there a column "H" which is either blank or contains a series of numbers that is separated by commas. What i would like to do is for the program to read this particular cell (Ie. say "H3") which would contain something like "18-20001442, 18-20000298, 17-20000477" and see that there are commas and replace the last one in this sequence with an "&" symbol so the new entry into the word document would be "18-20001442, 18-20000298 & 17-20000477". Please let me know if this is possible or what is the best way of doing this.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Assuming all of your commas are always followed by a space, select the cells you want to modify and then run this macro...
VBA Code:
Sub LastCommaBecomesAmpersand()
  Dim Cell As Range
  For Each Cell In Selection
    If Cell.Value Like "*,*" And Not Cell.Value Like "*&*" Then Cell.Value = Application.Substitute(Cell.Value, ",", " &", Len(Cell.Value) - Len(Replace(Cell.Value, ",", "")))
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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