How to TRIM away unwanted data from a cell

meppwc

Well-known Member
Joined
May 16, 2003
Messages
604
Office Version
  1. 365
Platform
  1. Windows
I have a column that, that if data exists, then I want to trim away everything except for a 7 digit number (if a 7 digit number exists)
The cells can contain, letters, numbers (other than 7 digits next to each other), or be blank

Is there any way to trim away everything except for the 7 digit number (if exists)?
 
Just so you know, NeonRedShapie's UDF will return a value for text like this (as well as other variations)...

sda 1.2,3-$ dsk

As long as your numbers are whole numbers (no decimal point), here is a routine that is immune to those problems and it works whether the number is surrounded by spaces or not (use it the same way, although note the name change)...
Code:
Function GetNumber(S As String, Count As Long) As Variant
  Dim X As Long
  For X = 1 To Len(S) - Count + 1
    If Mid(S, X, Count) Like String(Count, "#") Then
      GetNumber = Mid(S, X, Count)
      Exit Function
    End If
  Next
  GetNumber = ""
End Function
 
Last edited:
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,215,372
Messages
6,124,531
Members
449,169
Latest member
mm424

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