Remove first blank line break in Excel using VBA

rdoulaghsingh

Board Regular
Joined
Feb 14, 2021
Messages
105
Office Version
  1. 365
Platform
  1. Windows
I'm using the below script to remove only blank line breaks using Chr(10). It works great, except that it doesn't remove the line break when it occurs at the beginning of the cell. I only need it to remove the first line break carriage from the top of the cell.

VBA Code:
Sub RemoveBlankLines()
    Columns("W").Replace What:=Chr(10) & Chr(10), Replacement:=Chr(10), _
        LookAt:=xlPart, SearchFormat:=False, ReplaceFormat:=False
End Sub

Any help is appreciated! Thanks!
 
So you are double-clicking a cell and you want the cell in the next column to have its leading Line Feed removed? If so...

Target.Offset(, 1).Value = Mid(Target.Offset(, 1).Value, 2)
@Rick Rothstein Thank you so much for your continued effort. Unfortunately, this won't work pass the first review. When I double click on the review cell, it works the very first time. If the same person double clicks again to update their review it removes the first character and then enters their name on the second double click. Maybe I need to take a different approach. Is there an If statement I can use like <If Char(10) = "" Then delete it> or something with isblank?
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
@Rick Rothstein Thank you so much for your continued effort. Unfortunately, this won't work pass the first review. When I double click on the review cell, it works the very first time. If the same person double clicks again to update their review it removes the first character and then enters their name on the second double click. Maybe I need to take a different approach. Is there an If statement I can use like <If Char(10) = "" Then delete it> or something with isblank?
Okay, try it this way...
VBA Code:
If Left(Target.Offset(, 1), 1) = vbLf Then Target.Offset(, 1).Value = Mid(Target.Offset(, 1).Value, 2)

By the way, you told me there would always be a Line Feed to delete. Perhaps the following message that I have posted in the past will give you an insight into things from my side of the conversation...

One thing you must keep in mind when you ask a question in a forum... the people you are asking to help you know absolutely nothing about your data, absolutely nothing about how it is laid out in the workbook, absolutely nothing about what you want done with it and absolutely nothing about how whatever it is you want done is to be presented back to you as a result... you must be very specific about describing each of these areas, in detail, and you should not assume that we will be able to "figure it out" on our own. Remember, you are asking us for help... so help us to be able to help you by providing the information we need to do so, even if that information seems "obvious" to you (remember, it is only obvious to you because of your familiarity with your data). To sum up... we only know what you tell us, nothing more.
 
Upvote 0
Solution
Okay, try it this way...
VBA Code:
If Left(Target.Offset(, 1), 1) = vbLf Then Target.Offset(, 1).Value = Mid(Target.Offset(, 1).Value, 2)

By the way, you told me there would always be a Line Feed to delete. Perhaps the following message that I have posted in the past will give you an insight into things from my side of the conversation...

One thing you must keep in mind when you ask a question in a forum... the people you are asking to help you know absolutely nothing about your data, absolutely nothing about how it is laid out in the workbook, absolutely nothing about what you want done with it and absolutely nothing about how whatever it is you want done is to be presented back to you as a result... you must be very specific about describing each of these areas, in detail, and you should not assume that we will be able to "figure it out" on our own. Remember, you are asking us for help... so help us to be able to help you by providing the information we need to do so, even if that information seems "obvious" to you (remember, it is only obvious to you because of your familiarity with your data). To sum up... we only know what you tell us, nothing more.
@Rick Rothstein That worked perfectly! Thank you! My apologies for the incorrect info. I've been working on scripting different parts of this all weekend and my brain is fried. You are absolutely right and I will keep that in mind and try to be more diligent when asking for help. Again...thanks for all your willingness to help and swift reply on this.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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