Find carriage return in cell & highlight row

RigMan

New Member
Joined
Jun 14, 2007
Messages
6
I know there's been discussions here before on how to search for carriage returns, but my needs are a little different, and being a newbie, I can't seem to figure it out myself.

I have a spreadsheet where one of the cells should contain only a single part number. But to save time, in certain instances the user has put one part number, hit Alt-Enter and entered a second part number. They do this because all the other information on the row is identical for both parts.

Now I need to search all cells for carriage returns, and if found, do one of two things;

Option A - Highlight the row in yellow. Then a user can go thru and manually copy the row, and change the existing row and the new row to contain a single part number only, or....

Option B - Automatically insert a new row after the offending row, and copy and paste the row. Then highlight both rows for the user to modify. This would at least save the user a little time.

Note there is also other columns that could contain a carriage return as well. I'd like to copy those rows as well. So all cells would need to be searched and either option A or B performed. But if multiple cells in the same row have Carriage returns, I only want to copy the row once.

Thanks in advance,
Ronnie
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Ronnie

You could use conditional formatting for this (in the example, I selected A1:B6 and went Format>Conditional Formatting):

Excel Workbook
AB
1NameOther
2RichardSchollar1 House
3Jeremy2 Houses
4Daniel1 Car
5DonaldTrump10 Houses20 Cars
6Samuel1 House1 Car
Sheet3
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A11. / Formula is =COUNTIF(1:1,"*"&CHAR(10)&"*")>0Abc
B11. / Formula is =COUNTIF(1:1,"*"&CHAR(10)&"*")>0Abc
A21. / Formula is =COUNTIF(2:2,"*"&CHAR(10)&"*")>0Abc
B21. / Formula is =COUNTIF(2:2,"*"&CHAR(10)&"*")>0Abc
A31. / Formula is =COUNTIF(3:3,"*"&CHAR(10)&"*")>0Abc
B31. / Formula is =COUNTIF(3:3,"*"&CHAR(10)&"*")>0Abc
A41. / Formula is =COUNTIF(4:4,"*"&CHAR(10)&"*")>0Abc
B41. / Formula is =COUNTIF(4:4,"*"&CHAR(10)&"*")>0Abc
A51. / Formula is =COUNTIF(5:5,"*"&CHAR(10)&"*")>0Abc
B51. / Formula is =COUNTIF(5:5,"*"&CHAR(10)&"*")>0Abc
A61. / Formula is =COUNTIF(6:6,"*"&CHAR(10)&"*")>0Abc
B61. / Formula is =COUNTIF(6:6,"*"&CHAR(10)&"*")>0Abc


EDIT: having selected A1:B6 and gone Format>Conditional Formatting, you only need to enter the following formula and then select your desired format (the other cells in A1:B6 will have the right formula applied to them)

Code:
=COUNTIF(1:1,"*"&CHAR(10)&"*")>0
 
Upvote 0
thanks Richard. That looks like it will work. I have never used conditional formatting before. If someone before me had applied conditional formating, is there a way to know where conditional formating has been applied?

For example, I used the conditional formating to apply cell shade yellow. Now that my row is yellow, if I didn't know conditional formating had been applied and try to go into Format-Cells and remove the yellow shading, it won't take. Because the conditional formating is overriding. How would I know that conditional formating is being used?
 
Upvote 0
Just for my education, my attempt at doing this with a macro is this;

Code:
Sub findrow()
Dim ws1 As Worksheet
Dim lastRw As Long, newRw As Long, i As Long
Dim lookFor As String
Dim found As Integer

lookFor = vbCrLf

Set ws1 = Sheets("Sheet1")

With ws1

lastRw = .Cells.Find(What:="*", _
After:=.Range("A1"), LookIn:=xlValues, _
SearchDirection:=xlPrevious).Row

For i = 1 To lastRw
    On Error Resume Next
    found = 0
    found = .Rows(i).Find(What:=lookFor, _
    After:=.Range("A" & i), LookIn:=xlValues, _
    SearchDirection:=xlNext)
    If found <> 0 Then
       ' highlight row here
    End If
Next

End With

End Sub

It doesn't seem to be finding the rows thought. And I don't know the syntax to highlight the row.
 
Upvote 0
Bump

Someone please look at my code above and tell me how to change to search for carriage returns, and how to change the shading on the row once found. Please???????
 
Upvote 0
OK first things first:

How do you know CF has been applied? Maybe by using a helper column that contains exactly the same test as used in the CF formula itself - if True, then CF is applied, if False then CF isn't applied etc.

I'm pretty sure your code should work BUT! you aren't simply looking for a LineFeed character (ie Ascii character 10) - you are seraching on a vbCrLf which is a pairing of a Carriage Return (Ascii character 13) and a LineFeed. The code will probably work if you use vbLf instead - as this is only a LineFeed character.

Hope this helps!

Richard
 
Upvote 0
Richard
I tried vbLf, vbCrLf, and vbCr. None worked correctly. I even tried searching for plain text that I knew was in one of the cells, and it didn't find it. So I think my logic is flawed somewhere.

Your solution using the Conditional Formatting has worked fine for me. But it would be nice to figure out the vb macro as well. I thought the problem might be in the syntax of my Find function. Specifically, the After clause.
 
Upvote 0
Declare found as a range and amend the code in the relevant parts to:

Code:
Dim found as Range

'...

Set found = .Rows(i).Find(What:=lookFor, _ 
     LookIn:=xlValues, Lookat:=xlPart, _
    SearchDirection:=xlNext)
If Not found Is Nothing 'ie lookfor's been found
 'apply formatting
End If

I didn't read carefully enough - Find returns an object if something is found, so you need your variable properly declared to handle it. The LookAt bit makes sure it will find matches within a cell. I deleted the After bit, as I don't think it was important.

Hope this works!
 
Upvote 0
Okay, that worked. Thanks Richard.

Here is the final code in case someone else needs it. It searchs all cells for a line feed and changes the background color of the row to yellow if found.

Code:
Sub findrow()
Dim ws1 As Worksheet
Dim lastRw As Long, newRw As Long, i As Long, p As Long
Dim lookFor As String
Dim found As Range

lookFor = vbLf

Set ws1 = Sheets("Sheet1")

With ws1

lastRw = .Cells.Find(What:="*", _
After:=.Range("A1"), LookIn:=xlValues, _
SearchDirection:=xlPrevious).Row

For i = 1 To lastRw
    On Error Resume Next
Set found = .Rows(i).Find(What:=lookFor, _
     LookIn:=xlValues, Lookat:=xlPart, _
    SearchDirection:=xlNext)
If Not found Is Nothing Then 'ie lookfor's been found
 'apply formatting
 .Rows(i).Interior.ColorIndex = 27
End If

Next

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,218,263
Messages
6,141,431
Members
450,357
Latest member
Pritch_0

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