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!
 
@Rick Rothstein Yes - the code I'm using adds the first line as Char10. At the end of the execution after the username is added, I want to remove that first empty blank line feed only.
So now I am not sure where in your code you want or need this to happen so, for example purposes, I'll use Cell as the variable name (it may actually be Target if inside an event procedure or Selection if you are trying to do this to a selected cell). To remove a leading Line Feed...

If you know for sure there is always a leading LineFeed, then...
V = Mid(V, 2)

If there might be a Line Feed but also might not be one, then...
If Left(V, 1) = vbLf Then V = Mid(V, 2)
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
So now I am not sure where in your code you want or need this to happen so, for example purposes, I'll use Cell as the variable name (it may actually be Target if inside an event procedure or Selection if you are trying to do this to a selected cell). To remove a leading Line Feed...

If you know for sure there is always a leading LineFeed, then...
V = Mid(V, 2)

If there might be a Line Feed but also might not be one, then...
If Left(V, 1) = vbLf Then V = Mid(V, 2)
@Rick Rothstein At the end of the procedure it activates the cell I would like to remove the leading blank linefeed from. And yes. it always produces a leading blank linefeed. The code would be inserted where it says <Code Here> below:

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

On Error Resume Next

If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("V1:V1309")) Is Nothing Then
 
   answer = MsgBox("Mark row as reviewed?", vbQuestion + vbYesNo + vbDefaultButton1, Environ("username"))
   If answer = vbYes Then

Application.ScreenUpdating = False

Application.DisplayAlerts = False

      With Target
         If InStr(1, .Offset(0, 1).Value, Environ("username")) = 0 Then
         .Offset(0, 1).Value = .Offset(0, 1).Value & Chr(10) & Environ("username")
         End If
         .Offset(0, 2) = " Last reviewed by: " & Environ("username") & " " & Now()
         Target.Offset(0, 1).Select
         'Selection.Value = Replace(Selection.Value, vbLf & vbLf, vbLf, , 1)
         <<<CODE HERE>>>
         'Msg = CreateObject("WScript.Shell").PopUp(ActiveSheet.Name & " updated successfully.", 1, "Info.")
         Cancel = True
      End With
    Else

Target.Offset(0, 1).Select

    End If
   
End If

'RemoveLineBreak

Application.ScreenUpdating = True

Application.DisplayAlerts = True
 
End Sub
 
Upvote 0
Ah, you are inside an event procedure (your first post showed you inside a macro), try replacing Selection with Target...

Target.Value = Replace(Target.Value, vbLf & vbLf, vbLf, , 1)
@Rick Rothstein Thanks again! I gave it a try, but it's removing a character from the cell to the left of the target (image attached).
 

Attachments

  • image_2022-05-01_174154842.png
    image_2022-05-01_174154842.png
    10.2 KB · Views: 7
Upvote 0
@EXCEL MAX Thanks for the code. This one might be a little bit out of my wheelhouse. I'm new to VBA. I just need a one-liner to remove a blank linefeed in the first line of the active cell.
 
Upvote 0
It's OK. We often meets cases like this when is hard to explain problem,
and also is hard to us to determine level of people's knowledge.
Hope so you are successfully resolve this small problem.
 
Upvote 0
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)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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