Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default excel, seperating numbers and text, macro

I have a file with lines like these

nominally Roman Catholic 92% (less than 20% practicing)
Armenian Apostolic 94.7%
Roman Catholic 80.8%
Eastern Orthodox 80%
Roman Catholic 75%
Buddhist 9.3%
Baptist 42%

my file has 250 lines. I want to separate the data from the text. Is there
any way to do this other than manually going through each line and separating
the data? I considered a macro but each line is different. In some cases
the number has 2 digits and no decimals but in other cases it has decimals.

thanks



  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,510
Default excel, seperating numbers and text, macro

Hi Gene,

The following code will separate the first percentage value into a separate
column and remove it from the first column. I have not handled the "(less
than 20% practicing)" because not sure what you want to do with it. Maybe it
is OK left in the original data but let me know.

Note the comments where you might have to edit the code to suit.

Ensure you back up your data before running the code in case it does not do
exactly what you expect.

Sub SeparatePercentages()
Dim rngToSeparate As Range
Dim cel As Range
Dim i As Long
Dim strChar As String
Dim strTemp As String

'Edit sheet name to suit your sheet name
With Sheets("Sheet1")
'Edit the following range to suit range of your data
Set rngToSeparate = .Range("A1:A7")
End With

'Edit following line to suit required column for results
'It must be the next column to the right of the original data
Columns("B:B").NumberFormat = "0.00%"


For Each cel In rngToSeparate
strTemp = ""
For i = 1 To Len(cel.Value)
strChar = Mid(cel.Value, i, 1)

Select Case strChar
Case 0 To 9, ".", "%"
strTemp = strTemp & strChar
If strChar = "%" Then Exit For
End Select
Next i

'Extract value only from variable without % sign
cel.Offset(0, 1) = Val(Left(strTemp, Len(strTemp) - 1)) / 100

'Delete the value and percentage sign from original data
cel.Value = Replace(cel.Value, " " & strTemp, "")
Next cel

End Sub


--
Regards,

OssieMac


"Gene Shackman" wrote:

I have a file with lines like these

nominally Roman Catholic 92% (less than 20% practicing)
Armenian Apostolic 94.7%
Roman Catholic 80.8%
Eastern Orthodox 80%
Roman Catholic 75%
Buddhist 9.3%
Baptist 42%

my file has 250 lines. I want to separate the data from the text. Is there
any way to do this other than manually going through each line and separating
the data? I considered a macro but each line is different. In some cases
the number has 2 digits and no decimals but in other cases it has decimals.

thanks



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Location: Bakersfield, CA
Posts: 45
Send a message via Skype™ to JBeaucaire[_85_]
Default excel, seperating numbers and text, macro

This simple FUNCTION will strip the letters out of a cell leaving everything
else.
=========
Function LetterOut(rng As Range)
Dim i As Integer
For i = 1 To Len(rng)
Select Case Asc(Mid(rng.Value, i, 1))
Case 0 To 64, 123 To 197

LetterOut = LetterOut & Mid(rng.Value, i, 1)
End Select
Next i
End Function
==========
Paste that into a MODULE. Use it as:
=LetterOut(A1)

....or to remove the spaces left in the
=TRIM(letterout(A1))

Here's another function to strip out numerals:
==========
Function StripNumber(stdText As String)
Dim str As String, i As Integer
'strips the number from a longer text string
stdText = Trim(stdText)

For i = 1 To Len(stdText)
If Not IsNumeric(Mid(stdText, i, 1)) Then
str = str & Mid(stdText, i, 1)
End If
Next i

StripNumber = str ' * 1
End Function
============

Use it as:
=StripNumber(A1)

Now, in your text there are periods and percent symbols left over, so I
added some color to strip that out in this final formula:
=TRIM(SUBSTITUTE(SUBSTITUTE(stripnumber(A1),".","" ),"%",""))

--
"Actually, I AM a rocket scientist." -- JB


"Gene Shackman" wrote:

I have a file with lines like these

nominally Roman Catholic 92% (less than 20% practicing)
Armenian Apostolic 94.7%
Roman Catholic 80.8%
Eastern Orthodox 80%
Roman Catholic 75%
Buddhist 9.3%
Baptist 42%

my file has 250 lines. I want to separate the data from the text. Is there
any way to do this other than manually going through each line and separating
the data? I considered a macro but each line is different. In some cases
the number has 2 digits and no decimals but in other cases it has decimals.

thanks



  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default excel, seperating numbers and text, macro

On Sun, 11 Jan 2009 21:13:00 -0800, Gene Shackman <Gene
wrote:

I have a file with lines like these

nominally Roman Catholic 92% (less than 20% practicing)
Armenian Apostolic 94.7%
Roman Catholic 80.8%
Eastern Orthodox 80%
Roman Catholic 75%
Buddhist 9.3%
Baptist 42%

my file has 250 lines. I want to separate the data from the text. Is there
any way to do this other than manually going through each line and separating
the data? I considered a macro but each line is different. In some cases
the number has 2 digits and no decimals but in other cases it has decimals.

thanks



Not sure exactly what you mean.

The following macro will
operate on a range of selected cells
clear a few cells adjacent to the selected cells
place the numeric values into the adjacent cells (including % signs)

The values are extracted as text strings.

==================================
Option Explicit
Sub GetData()
Dim c As Range
Dim i As Long
Dim re As Object, mc As Object, m As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "[\-+]?\b\d*\.?\d+\b%?"

For Each c In Selection
Range(c(1, 2), c(1, 3)).ClearContents 'clear right of data
If re.test(c.Value) = True Then
Set mc = re.Execute(c.Value)
For i = 0 To mc.Count - 1
c(1, i + 2).Value = mc(i)

Next i
End If
Next c
End Sub
======================================

If you want to extract these as numbers, and divide by 100 if the % sign is
present, then use this:

===========================
Option Explicit
Sub GetData()
Dim c As Range
Dim i As Long
Dim re As Object, mc As Object, m As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "[\-+]?\b\d*\.?\d+\b%?"

For Each c In Selection
Range(c(1, 2), c(1, 3)).ClearContents 'clear right of data
If re.test(c.Value) = True Then
Set mc = re.Execute(c.Value)
For i = 0 To mc.Count - 1
If Right(mc(i), 1) = "%" Then
c(1, i + 2).Value = Left(mc(i), Len(mc(i)) - 1) / 100
Else
c(1, i + 2).Value = CDbl(mc(i))
End If
Next i
End If
Next c
End Sub
==================================

--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seperating numbers from text mns Excel Discussion (Misc queries) 2 July 24th 08 10:52 AM
Parsing / seperating text string in excel cell sfleck Excel Discussion (Misc queries) 7 November 16th 07 12:24 AM
Seperating text Richard Excel Discussion (Misc queries) 8 June 29th 06 10:21 PM
Seperating Numbers from Letters in Excel Tel Excel Worksheet Functions 7 March 23rd 06 11:36 PM
Seperating text if there's more than a one space between them Joey Excel Discussion (Misc queries) 3 January 17th 06 07:41 PM


All times are GMT +1. The time now is 09:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"