Solved

Excel 2007 Removing Carriage Returns In a Cell

Posted on 2015-01-29
9
101 Views
Last Modified: 2015-02-25
Hello,

Hoping someone can assist by creating a formula or VB code that will remove all carriage returns within a cell for an entire column.  For example, cell B2 has 3 blank returns, text, then 2 blank returns, and more text.  I would like the blank returns deleted.  Any thoughts on how to remove for the entire B column?

Thanks!
0
Comment
Question by:Escanaba
9 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 40577913
This code should do it

Sub ReplaceCR()
Dim cCell As Range

For Each cCell In Range(Range("B1"), Range("B" & Rows.Count).End(xlUp))
    cCell = Replace(cCell, Chr(13), "")
Next cCell

End Sub

Open in new window



gowflow
0
 
LVL 5

Accepted Solution

by:
Hakan Yılmaz earned 500 total points
ID: 40577916
Please try this.
DeleteCR will delete carriage returns in selected range.
ReduceCR will replace double CRs with one.

Sub DeleteCR()
    Selection.Replace What:=ChrW(10), Replacement:=""
End Sub

Sub ReduceCR()
    Selection.Replace What:=ChrW(10) & ChrW(10), Replacement:=ChrW(10)
End Sub

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 40577927
No points, please :)

Typically, to insert a new line with an Excel cell, the line feed character (ANSI 10) is used, not the carriage return (ANSI 13).  Thus, Hakan's approach is probably more in line with what you need.

If you want a non-macro approach:

1) Use a formula like this:
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))
That replaces line feeds with a single space, and TRIM reduces consecutive spaces to a single space

2) Copy that formula as needed

3) If desired, use Copy / Paste Special / Values over the original data, and delete the area with the formula above
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40577986
This will reduce all repetitive CR's to 1 CR in selected range.
Sub ReduceTo1CR()
    Do
        Selection.Replace What:=ChrW(10) & ChrW(10), Replacement:=ChrW(10)
    Loop Until Selection.Find(ChrW(10) & ChrW(10)) Is Nothing
End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40578168
More generally, RemoveRepeating sub will replace a repetitive text with single ones in specified range.
Also a sub for prompting user input is added.
Sub ReduceTo1CR()
    RemoveRepeating Selection, ChrW(10)
End Sub

Sub PromptRemoveRepeating()
    Dim promptrange As Range
    Dim promptstring As String
    On Error Resume Next
    Set promptrange = Application.InputBox("Select the range from which you want to remove repetitive text", "RemoveRepeating", , , , , , 8)
    If promptrange Is Nothing Then
        MsgBox "No range selected."
        Exit Sub
    End If
    promptstring = InputBox("Enter single text for replacing repetitives.", "RemoveRepeating")
    If Len(promptstring) = 0 Then
        MsgBox "No text written."
        Exit Sub
    End If
    RemoveRepeating promptrange, promptstring
End Sub

Sub RemoveRepeating(ByRef myrange As Range, ByVal mystring As String)
    Do
        myrange.Replace What:=mystring & mystring, Replacement:=mystring
    Loop Until myrange.Find(mystring & mystring) Is Nothing
End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40628844
I've requested that this question be closed as follows:

Accepted answer: 500 points for gowflow's comment #a40577913

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40628845
Please review the question and answers. Question is about replacing not needed multiple blank returns with one.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question