Extracting only text from a string containing text and numbers

PGInsight

New Member
Joined
Jun 13, 2013
Messages
13
Hi can anybody give me a formula that will extract only the text from these examples

DEBDEN - 12
EAST ARNDALE - 150
LITTLEHAMPTON - 2999

<tbody>
</tbody><colgroup><col></colgroup>


<tbody>
</tbody><colgroup><col></colgroup>

<tbody>
</tbody><colgroup><col></colgroup>
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

go to the Excel Visual Basic editor (ALT+F11) and insert this in a module:
Code:
Function OnlyText(Ran As String) As StringDim StringCount As Integer
StringCount = 1
Do Until Mid(Ran, StringCount, 1) = "-"
    StringCount = StringCount + 1
Loop
Do Until Mid(Ran, StringCount + 1, 1) = " " And Mid(Ran, StringCount, 1) <> "-"
    StringCount = StringCount - 1
Loop


OnlyText = Left(Ran, StringCount)


End Function
You can insert a module by selecting Insert-->Module in the menu bar.

you can now use the formula: =OnlyText(A1) in your spreadsheet.

Hope this helps!

Maagaard
 
Upvote 0
PGInsight,
WElcome to MrExcel.

If your examples are tyical in that the - separates the text from the numbers then you can use....

=TRIM(LEFT(A1,FIND("-",A1)-1))

Hope that helps.
 
Upvote 0
hi thx for this, I'm getting a compile error when I run this 'Expected:end of statement'

For some reason, its pasted in to this page wrong change the top line: Function OnlyText(Ran As String) As StringDim StringCount As Integer
to:
Function OnlyText(Ran As String) As String
Dim StringCount As Integer

Be aware that this only works, as long as there is a - between text and numbers.
 
Upvote 0
go to the Excel Visual Basic editor (ALT+F11) and insert this in a module:
Code:
Function OnlyText(Ran As String) As StringDim StringCount As Integer
StringCount = 1
Do Until Mid(Ran, StringCount, 1) = "-"
    StringCount = StringCount + 1
Loop
Do Until Mid(Ran, StringCount + 1, 1) = " " And Mid(Ran, StringCount, 1) <> "-"
    StringCount = StringCount - 1
Loop

 OnlyText = Left(Ran, StringCount)
 
End Function
You can insert a module by selecting Insert-->Module in the menu bar.

you can now use the formula: =OnlyText(A1) in your spreadsheet.
Snakehips has posted a direct formula that the OP can use and that would be better for him to use than a UDF like your OnlyText; however, I thought you might be interested in seeing a UDF which does what yours does but using only one line of code...

Code:
Function OnlyText(Ran As String) As String
  OnlyText = Trim(Split(Ran, "-")(0))
End Function
 
Upvote 0
Be aware that this only works, as long as there is a - between text and numbers.

Here is my one-liner modified to handle that problem (I think you could have employed the same trick in your UDF to avoid the problem as well)...

Code:
Function OnlyText(Ran As String) As String
  OnlyText = Trim(Split(Ran & "-", "-")(0))
End Function
 
Upvote 0

Forum statistics

Threads
1,203,470
Messages
6,055,604
Members
444,803
Latest member
retrorocket129

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