How to remove all line breaks, leaving only single line breaks?

leonlai

Board Regular
Joined
Mar 22, 2019
Messages
77
Hi, Everybody

I want to compact all the comments on a worksheet to their minimum sizes because there are so many of them scattered everywhere on the worksheet.

One way comments may occupy too much space is when the user inserts unnecessary line feeds in the comment box.

I want to remove them with VBA.

I got this nice solution on the forum:

https://www.mrexcel.com/forum/excel-questions/873385-removing-consecutive-line-breaks.html

The code works perfectly, but it leaves me very puzzled.

The author, Rick Rothstein says his code can delete up to 12 blank lines.

I split his code into chunks to facilitate understanding:

Code:
     A = WorksheetFunction.Substitute(ctxt, WorksheetFunction.Rept(Chr(10), [B]5[/B]), Chr(10))
     B = WorksheetFunction.Substitute(A,    WorksheetFunction.Rept(Chr(10), [B]3[/B]), Chr(10))
     C = WorksheetFunction.Substitute(B,    WorksheetFunction.Rept(Chr(10), [B]3[/B]), Chr(10))
     D = WorksheetFunction.Substitute(C,    WorksheetFunction.Rept(Chr(10), [B]2[/B]), Chr(10))
    
        
     cmt.Shape.DrawingObject.Text = D


To test the code, I created a Comment with 12 blank lines (line feeds) and also text in separate lines
When I pressed the macro button, all the 12 LFs were successfully deleted!


What seems very mysterious to me:

On 1st iteration, it deletes 5 LF, replacing them by 1 LF. (Hence, Net = 4)
On 2nd iteration, it deletes 3 LF, replacing them by 1 LF. (Hence, Net = 2)
On 3rd iteration, it deletes 3 LF, replacing them by 1 LF. (Hence, Net = 2)
On 4th iteration, it deletes 2 LF, replacing them by 1 LF. (Hence, Net = 1)

Hence, the no. of blank lines deleted should be 4+2+2+1 = 9

Yet, 12 lines have been deleted!!

Can anybody explain to me what really happened?


Thanks
Leon
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I am replying to my own thread.

After being much puzzled, I found the answer.

My mistake was to believe that on the 1st iteration, it was replacing 5 LF with 1 LF.

No!

It is replacing all occurrences of 5 LF by 1 LF each time.
There are 2 groups of 5 LF available. So, it deletes 10 LF and replaces by 2LF.

Continuing the same reasoning, I can now understand how the 12 blank rows are successfully deleted!

Thanks
Leon
 
Upvote 0
Hello,

Looks like you should have ...

Code:
[COLOR=#ff0000]B[/COLOR][COLOR=#333333] = WorksheetFunction.Substitute(A,    WorksheetFunction.Rept(Chr(10), [/COLOR][COLOR=#ff0000][B]4[/B][/COLOR][COLOR=#333333]), Chr(10))[/COLOR]

HTH
 
Upvote 0
Hi James

I copied the formula from the forum, and wondered why the author used 3 twice.

I think you are right: should be 4.

Thanks
Leon
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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