Domanda

I am trying to extract numbers from a text. If I have an entry like 12&6&2014, how can I extract the 12 (the number that is before the first &) and 2014 (the number that occurs after the second &)?

È stato utile?

Soluzione

To get first number:

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

To get last number after the second &:

=RIGHT(A1, 4)

Otherwise, if that's not always a year:

=MID(A1, FIND(CHAR(1), SUBSTITUTE(A1, "&", CHAR(1), 2))+1, LEN(A1))

Altri suggerimenti

you can loop through each character int he string and check to see if it is numeric

Sub getNumberValues()

Dim s As String
Dim c As New Collection
Dim sNewString As String
s = "12&6&2014"

For v = 1 To Len(s)

If IsNumeric(Mid(s, v, 1)) Then
    sNewString = sNewString & Mid(s, v, 1)
Else
c.Add sNewString
    sNewString = ""
End If

Next v
'add the last entry
c.Add sNewString
sNewString = ""
For Each x In c
    sNewString = sNewString & x & Chr(13)
Next

MsgBox sNewString
End Sub

If what I understand is correct which is that the characters that separate vary as well as how many digits are used you might look into something like this:

Function CleanUp(Txt)

For x = 1 To 255
    Select Case x
        Case 45, 47, 65 To 90, 95, 97 To 122
         Txt = WorksheetFunction.Substitute(Txt, Chr(x), "") <- "" can be replaced
     End Select                                                 with "&" to do a
 Next x                                                         MID() using & as
                                                                the delimiter
 CleanUp = Txt

 End Function

If you can use VBA, this will replace your characters with a blank, but you could put in your own character and then use your formulas to separate from a specific delimiter.

The original code can be found here:

http://www.mrexcel.com/forum/excel-questions/380531-extract-only-numbers.html

Simplest might be Text to Columns with & as the delimiter, then delete the middle column. This overwrites the original data, so a copy might be appropriate.

Another simple way would be to create two copies, and for one Find what: &* and Replace with: nothing, for the other Find what: *& and Replace with: nothing.

An alternative formula solution might be:

=DAY(SUBSTITUTE($A1,"&","/"))  

and

=YEAR(SUBSTITUTE($A1,"&","/"))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top