VBA: Delete lines in cells starting with an alphabet

jiobi

New Member
Joined
Feb 21, 2021
Messages
15
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
I am looking for a simple VBA code to Delete lines in cells starting with an alphabet.
In each cell in column c, there is a multiline text starting with numbers, special characters, and words.
I also delete the space before them if any. Some cells are empty.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
How about this
I assumed that the data is in column C and that column D is empty

VBA Code:
Sub jec()
 Dim ar, sp As Variant, sq As Long, i As Long, j As Long
 ar = Range("C1", Range("C" & Rows.Count).End(xlUp)).Resize(, 2)
 
 For i = 1 To UBound(ar)
     sp = Split(ar(i, 1), vbLf)
     For j = 0 To UBound(sp)
        If Not sp(j) = "" Then
           sq = Asc(Left(LTrim(sp(j)), 1))
            If sq < 65 Or sq > 90 And sq < 97 Or sq > 122 Then
                ar(i, 2) = ar(i, 2) & LTrim(sp(j)) & vbLf
            End If
            If j = UBound(sp) And Right(ar(i, 2), 1) = vbLf Then ar(i, 2) = StrReverse(Replace(StrReverse(ar(i, 2)), vbLf, "", 1, 1))
       End If
     Next
 Next
 
 Range("C1", Range("C" & Rows.Count).End(xlUp)) = Application.Index(ar, 0, 2)
End Sub
 
Upvote 0
A slightly different approach with an extra array. In this case it doesn't matter if Column D is empty or not

VBA Code:
Sub jec()
 Dim ar, jv, sp As Variant, sq As Long, i As Long, j As Long
 ar = Range("C1", Range("C" & Rows.Count).End(xlUp))
 ReDim jv(1 To UBound(ar))
 
 For i = 1 To UBound(ar)
     sp = Split(ar(i, 1), vbLf)
     For j = 0 To UBound(sp)
        If Not sp(j) = "" Then
           sq = Asc(Left(LTrim(sp(j)), 1))
            If sq < 65 Or sq > 90 And sq < 97 Or sq > 122 Then
                jv(i) = jv(i) & LTrim(sp(j)) & vbLf
            End If
            If j = UBound(sp) And Right(jv(i), 1) = vbLf Then jv(i) = StrReverse(Replace(StrReverse(jv(i)), vbLf, "", 1, 1))
       End If
     Next
 Next
 
 Range("C1", Range("C" & Rows.Count).End(xlUp)) = Application.Transpose(jv)
End Sub
 
Upvote 0
Nope, it is not working sir.
Showing issue in this line: Range("C1", Range("C" & Rows.Count).End(xlUp)) = Application.Transpose(jv)
 
Upvote 0
Can you post some sample data? I tried it with my own sample data and it worked properly.

You probably have empty cells or cells without multiple lines
 
Upvote 0
Also if I can delete the lines with "[" if anywhere then it will also fulfill my need because it will delete the lines in cells starting with an alphabet. Yes, there is space between short lines.
 
Upvote 0
If you could post some sample data I can look again
 
Upvote 0
Can't download this, you can post some sample data here as table or XL2BB
 
Upvote 0
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0

Forum statistics

Threads
1,224,937
Messages
6,181,859
Members
453,068
Latest member
DCD1872

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