Help with removing all 'new line' characters from range...

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I think I am dealing with Char 10 (new line). I have tried:
Code:
Cell.Value = Trim(Cell)
Code:
.UsedRange.Replace Chr(10), ""
neither works... the spaces are still there.

when clicking into one of the cells in the formula bar, I see this:

"CSR0o "

When I click at the end of that string, the cursor is only ONE space over from the "o".

(and here is where its behaving strangely...)

When I hit the 'backspace' key to manually remove the space, the FIRST backspace keystroke doesn't do anything... (the cursor doesn't move.)
Its when I hit it a second time that it removes the 'space'.
And no, there is not two trailing spaces at the end of each string (using the 'arrow' keys you only have to move the left 1 space before you are into the string.)

Very weird.

Thanks for any help/assistance!

PS-

hopefully the picture will do a better job explaining what im attempting to.... :)

wut final-1.PNG

wut final-2.PNG
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

If you put this in a blank cell (say Z4) and drag to the right until you get #value, what are the numbers you get?
Excel Formula:
=UNICODE(MID($E4,COLUMNS($Z4:Z4),1))
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

If you put this in a blank cell (say Z4) and drag to the right until you get #value, what are the numbers you get?
Excel Formula:
=UNICODE(MID($E4,COLUMNS($Z4:Z4),1))
Thank you for the suggestion to update my account info (its done.)

As far as the code, it did't like it.

wut3.PNG

wut4.PNG
 
Upvote 0
(I copied the formula into the entire column that had values to make sure the issue wasn't with just that cell and it gave me the same #NAME? result)
 
Upvote 0
Ok Unicode didn't appear until 2013, which is why you get #Name
What does the message box return if you run this
VBA Code:
Sub kbishop()
   Dim i As Long
   Dim msg As String
   
   For i = 1 To Len(Range("Y4"))
      msg = msg & AscW(Mid(Range("Y4").Value, i, 1)) & vbLf
   Next i
   MsgBox msg
End Sub
 
Upvote 0
Thanks for your help on this, btw. (I thought it was going to be something really simple... but thats what I get for thinking!)

wut5.PNG
 
Upvote 0
Ok, you have a space & a carriage return at the end of the text, try
VBA Code:
   With ActiveSheet.UsedRange
      .Replace vbCr, "", xlPart, , , , False, False
      .Replace " ", "", xlPart, , , , False, False
   End With
 
Upvote 0
Ok, you have a space & a carriage return at the end of the text, try
VBA Code:
   With ActiveSheet.UsedRange
      .Replace vbCr, "", xlPart, , , , False, False
      .Replace " ", "", xlPart, , , , False, False
   End With

Thank you, Fluff. That works... but I can only get it to clear the range of cells when I insert into a commandbutton. Ideally, I really would prefer it to clear out those characters when the module is running and after it pastes the data onto the worksheet;

Here is the code for when the data is copied-and-pasted onto the target worksheet "MTHLY" (including your new code):

Code:
' Clear ALL the cells of any previous data from the last calculation that was ran;
Worksheets("MTHLY").Activate
ActiveWorkbook.Worksheets("MTHLY").Range(Cells(3, 1), Cells(53, 38)).Select
Selection.ClearContents

' Copy new completed data calculations on the "FOURTHA" page;
Worksheets("FOURTHA").Activate
ActiveWorkbook.Worksheets("FOURTHA").Range(Cells(1, 77), Cells(25, 114)).Select
Selection.Copy

' Now paste data from the ("FOURTHA") worksheet onto the ("MTHLY") worksheet;
Worksheets("MTHLY").Activate
ActiveWorkbook.Worksheets("MTHLY").Range(Cells(3, 1), Cells(51, 38)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

' Clean the cells on the ("MTHLY")worksheet to remove carriage returns and spaces;
With ActiveSheet.UsedRange
     .Replace vbCr, "", xlPart, , , , False, False
     .Replace " ", "", xlPart, , , , False, False
End With

I also tried defining the range for the cells that I am trying to 'clean'... (which also doesn't work)

Code:
Dim tCol As Long
tCol = ActiveWorkbook.Worksheets("MTHLY").UsedRange.Rows.Count

With Worksheets("MTHLY").UsedRange
    ActiveWorkbook.Worksheets("MTHLY").Range(Cells(3, 1), Cells(tCol, 50)).Select
    .Replace vbCr, "", xlPart, , , , False, False
    .Replace " ", "", xlPart, , , , False, False
End With

However, this DOES WORK when its executed from a commandbutton that I placed onto the worksheet:

Code:
Private Sub cmdClean_Click()

Dim tCol As Long
tCol = ActiveWorkbook.Worksheets("MTHLY").UsedRange.Rows.Count

With Worksheets("MTHLY").UsedRange
    ActiveWorkbook.Worksheets("MTHLY").Range(Cells(3, 1), Cells(tCol, 50)).Select
    .Replace vbCr, "", xlPart, , , , False, False
    .Replace " ", "", xlPart, , , , False, False
End With
End Sub
 
Upvote 0
What if you try
VBA Code:
With ActiveWorkbook.Sheets("MTHLY").UsedRange
     .Replace vbCr, "", xlPart, , , , False, False
     .Replace " ", "", xlPart, , , , False, False
End With
 
Upvote 0
What if you try
VBA Code:
With ActiveWorkbook.Sheets("MTHLY").UsedRange
     .Replace vbCr, "", xlPart, , , , False, False
     .Replace " ", "", xlPart, , , , False, False
End With
Nope. Unfortunately that did not work either.

Also, I didnt mention it, but both characters are still there (requires 2 keystrokes to remove them.)

(I just thought I should mention that in order to confirm that one of them wasnt removed and not the other... )

Thanks for your help on this...
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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