![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Feb 2002
Posts: 20
|
C2 contains text that may or may not begin, end, or end&begin with a comma. The text may also have commas in the middle. I want to only strip the commas if they appear on either end.
,foo,bar, -> foo,bar ,foo,bar -> foo,bar foo,bar, -> foo,bar ,, -> I've been able to accomplish this task with multiple cells, but I figure somebody else could greatly improve upon my feeble attempt. In B2 I use =IF(RIGHT(C2,1)=",",REPLACE(C2,LEN(C2),1,""),C2) to strip off trailing commas and In A2 I use =IF(LEFT(B2,1)=",",REPLACE(B2,1,1,""),B2) to strip off leading commas. In general I'm having trouble figuring out how to have multiple IFs work against a text string. TIA. |
|
|
|
|
|
#2 |
|
Banned
Join Date: Feb 2002
Posts: 1,582
|
Hi
Try this way: =MID(C2,IF(FIND(",",C2)=1,2,1),IF(RIGHT(C2)=",",LEN(C2)-2,LEN(C2))) |
|
|
|
|
|
#3 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
|
Quote:
How about: =IF(AND(LEFT(A1)=",",RIGHT(A1)=","),MID(A1,2,LEN(A1)-2),IF(LEFT(A1)=",",MID(A1,2,LEN(A1)),IF(RIGHT(A1)=",",LEFT(A1,LEN(A1)-1),A1))) where A1 houses the target string. Aladin |
|
|
|
|
|
|
#4 |
|
BatCoder
Join Date: Feb 2002
Location: Turkey
Posts: 764
|
=IF(LEFT(C2,1)=",",IF(RIGHT(RIGHT(C2,LEN(C2)-1),1)=",",LEFT(RIGHT(C2,LEN(C2)-1),LEN(RIGHT(C2,LEN(C2)-1))-1),C2),IF(RIGHT(C2,1)=",",LEFT(C2,LEN(C2)-1),C2))
regards suat |
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
|
=SUBSTITUTE(TRIM(SUBSTITUTE(A1,","," "))," ",",")
|
|
|
|
|
|
#6 | ||
|
MrExcel MVP
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
|
Skebo,
Use the shorter formula Mark suggested. I believe those IFs are not entirely wasted as you were interested in its use explicitly. A small compensation I'd say Aladin Quote:
|
||
|
|
|
|
|
#7 |
|
New Member
Join Date: Feb 2002
Posts: 20
|
Sorry, I forgot to mention a case that obviously makes a big difference
foo,,bar -> foo,bar foo,,bar, -> foo,bar foo,bar,, -> foo,bar ,,,foo,,bar,, -> foo,bar My deepest apologies for leaving this important detail out. |
|
|
|
|
|
#8 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
|
Quote:
...will work for these cases as well. |
|
|
|
|
|
|
#9 |
|
New Member
Join Date: Feb 2002
Posts: 20
|
Mark,
I must say that is very slick. Thanks a bunch! |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|