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
CURRENT
WANTED RESULTS
APPLE JUICE
GRAPE JUICE
ORANGE JUICE,
APPLE JUICE, GRAPE JUICE, ORANGE JUICE
HOT DOG
BURGER
HOT DOG, BURGER
BLUE
GREEN
YELLOW,
BLUE, GREEN, YELLOW
WINTER
SUMMER
SPRING
FALL,
WINTER, SUMMER, SPRING, FALL
HOT, COLD, WARM, UNKNOWN
HOT, COLD, WARM, UNKNOWN

<tbody>
</tbody>
 
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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,216,419
Messages
6,130,515
Members
449,585
Latest member
kennysmith1

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