Remove lines not starting with numeric character

jack71

New Member
Joined
Aug 4, 2019
Messages
3
Hi,

how can I remove lines within each cell (multiple lines with line breaks) that are not starting with a numeric character?
223213 - this line needs to stay
chair - this line needs to be removed
9 - this line needs to stay
needs to be removed

<tbody>
</tbody>

Thanks and best,
Jack
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Here is a UDF (user defined function) that you can use...
Code:
Function NumStarts(S As String) As String
  Dim X As Long, Arr As Variant, Nums As Variant
  Arr = Split(S, vbLf)
  For X = 0 To UBound(Arr)
    If Arr(X) Like "[!0-9]*" Then Arr(X) = ""
  Next
  NumStarts = Join(Arr, vbLf)
  Nums = Split("121 13 5 3 3 2")
  For X = 0 To 5
    NumStarts = Replace(NumStarts, String(Nums(X), vbLf), vbLf)
  Next
  If Right(NumStarts, 1) = vbLf Then NumStarts = Left(NumStarts, Len(NumStarts) - 1)
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use NumStarts just like it was a built-in Excel function. For example,

=NumStarts(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Upvote 0
Here is a macro you can try. It assumes data in column A, starting at row 2 and results into column B.
If you want to overwrite the original data with the new, just remove the red part of the code.

Rich (BB code):
Sub Remove_Lines()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.MultiLine = True
  RX.Pattern = "^\D.*?(" & vbLf & "|$)"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      a(i, 1) = RX.Replace(a(i, 1), "")
    Next i
    .Offset(, 1).Value = a
  End With
End Sub

My sample data in A, result of code in B

Excel Workbook
AB
1Data
2223213 - this line needs to staychair - this line needs to be removed9 - this line needs to stayneeds to be removed223213 - this line needs to stay9 - this line needs to stay
3x23213 - this line now needs to gochair - this line needs to be removed9 - this line needs to stayneeds to be removed9 - this line needs to stay
4abcd
5
62342 text2342 text
723 stay45 also stay23 stay45 also stay
Remove Lines
 
Upvote 0
Re: Remove lines not starting with numeric character - SOLVED

Hi,

I'm very sorry for cross-posting. Now read all the rules and will keep in mind next time.

Solution works perfectly. Thank you very much for quick and prompt solution.

Best,
Jack
 
Upvote 0
Re: Remove lines not starting with numeric character - SOLVED

Solution works perfectly.
Solution singular? Two solutions have been offered to you... which one are you referring to? did you try the other one?
 
Upvote 0
Hi Rick,

thank you again both of you for your effort and sorry for late answer.

Both solutions with UDF and VBA works perfectly.

Thanks and best,
Jack
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

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