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]
 
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.
See if this code does what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub ReplaceLFwithCommaSpaceAfterRemovingTrailingComma()
  Dim Txt As Variant
  Application.ScreenUpdating = False
  With Range("E1", Cells(Rows.Count, "E").End(xlUp))
    .Value = Evaluate(Replace("IF({1},SUBSTITUTE(LEFT(TRIM(@),LEN(@)-(RIGHT(@)="","")),CHAR(10),"", ""))", "@", .Address))
    .Value = Evaluate(Replace("IF({1},IF(LEFT(@)="","",TRIM(MID(@,2,LEN(@)-1)),@))", "@", .Address))
  End With
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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