Solved

Excel 2007 Removing Carriage Returns In a Cell

Posted on 2015-01-29
9
97 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 45

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now