Replace Carriage Returns & Remove Last Comma

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
331
Office Version
  1. 365
Platform
  1. Windows
Using VBA is there a way to replace carriage returns with a comma space?

Also, if the value ends with comma space or comma i''d like to delete those value.

This will be done on sheet called Cranberries in coulmn E only.

EXAMPLE
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]CURRENT
[/TD]
[TD]WANTED RESULTS
[/TD]
[/TR]
[TR]
[TD]APPLE JUICE
GRAPE JUICE
ORANGE JUICE,
[/TD]
[TD]APPLE JUICE, GRAPE JUICE, ORANGE JUICE
[/TD]
[/TR]
[TR]
[TD]HOT DOG
BURGER
[/TD]
[TD]HOT DOG, BURGER
[/TD]
[/TR]
[TR]
[TD]BLUE
GREEN
YELLOW,
[/TD]
[TD]BLUE, GREEN, YELLOW
[/TD]
[/TR]
[TR]
[TD]WINTER
SUMMER
SPRING
FALL,
[/TD]
[TD]WINTER, SUMMER, SPRING, FALL
[/TD]
[/TR]
[TR]
[TD]HOT, COLD, WARM, UNKNOWN
[/TD]
[TD]HOT, COLD, WARM, UNKNOWN
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]APPLE JUICE
GRAPE JUICE
ORANGE JUICE,[/td][td]APPLE JUICE, GRAPE JUICE, ORANGE JUICE[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet26[/td][/tr][/table]

User defined function in cell B1:
=ReplaceReturn(A1)


Code:
Function ReplaceReturn(rng As Range)
Dim str As String
str = Trim(Replace(rng, Chr(10), ", "))
If Right(str, 1) = "," Then
    ReplaceReturn = Left(str, Len(str) - 1)
Else
    ReplaceReturn = str
End If
End Function
 
Upvote 0
How about
Code:
Sub repl()
   With Range("E1", Range("E" & Rows.Count).End(xlUp))
      .Replace Chr(10), ", ", xlPart, , , , False, False
      .Value = Evaluate(Replace("if(right(@,1)="","",left(@,len(@)-1),@)", "@", .Address))
   End With
End Sub
 
Upvote 0
How about
Code:
Sub repl()
   With Range("E1", Range("E" & Rows.Count).End(xlUp))
      .Replace Chr(10), ", ", xlPart, , , , False, False
      .Value = Evaluate(Replace("if(right(@,1)="","",left(@,len(@)-1),@)", "@", .Address))
   End With
End Sub

Thank you. This removed the return, but it leaves comma space (, ) at the end. Also, it placed some comma space at the beginning of some cells.
 
Upvote 0
[TABLE="class: head"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[TH][/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]C[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]D[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]E[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]F[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]G[/COLOR]​
[/TH]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]1[/COLOR]​
[/TD]
[TD]APPLE JUICE
GRAPE JUICE
ORANGE JUICE,[/TD]
[TD]APPLE JUICE, GRAPE JUICE, ORANGE JUICE[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]2[/COLOR]​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid"]
<tbody>[TR]
[TD]Sheet: Sheet26[/TD]
[/TR]
</tbody>[/TABLE]

User defined function in cell B1:
=ReplaceReturn(A1)


Code:
Function ReplaceReturn(rng As Range)
Dim str As String
str = Trim(Replace(rng, Chr(10), ", "))
If Right(str, 1) = "," Then
    ReplaceReturn = Left(str, Len(str) - 1)
Else
    ReplaceReturn = str
End If
End Function

<g class="gr_ gr_22 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="22" data-gr-id="22">Ive</g> never used function VBA codes. Not sure where to define the sheet or function.
 
Upvote 0
How about
Code:
Sub repl()
   With Range("B1", Range("B" & Rows.Count).End(xlUp))
      .Replace Chr(10), ", ", xlPart, , , , False, False
      .Value = Evaluate(Replace("if(right(@,2)="", "",left(@,len(@)-2),@)", "@", .Address))
      .Value = Evaluate(Replace("if(left(@,2)="", "",right(@,len(@)-2),@)", "@", .Address))
   End With
End Sub
 
Upvote 0
Here is another macro that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub ReplaceLFwithCommaSpaceAfterRemovingTrailingComma()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Offset(, 1) = Evaluate(Replace("IF({1},SUBSTITUTE(LEFT(TRIM(@),LEN(@)-(RIGHT(@)="","")),CHAR(10),"", ""))", "@", .Address))
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Here is another macro that you can consider...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub ReplaceLFwithCommaSpaceAfterRemovingTrailingComma()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Offset(, 1) = Evaluate(Replace("IF({1},SUBSTITUTE(LEFT(TRIM(@),LEN(@)-(RIGHT(@)="","")),CHAR(10),"", ""))", "@", .Address))
  End With
End Sub
[/TD]
[/TR]
</tbody>[/TABLE]


Thank you for this version of the code. Currently it is placing the results in the column to the right. Is there any way to keep the results in the current column? My data is in column E.

Also, some of my cells start with ", " (comma space without the "). Is there a way to delete these two leading charaters if the cell start with those? I can't do a simple find and replace cause I may have the comma space later in the cell. I just want to remove if the cell starts with that text.
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,103
Members
452,302
Latest member
TaMere

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