Excel - Extracting Digits From Text
Asked By Faraz A. Qureshi
09-Feb-10 07:55 AM
What could be wrong with the following piece of code for a UDF?
Function NFW(RNG As Range)
Dim RSLT As String
RSLT = ""
For X = 1 To Len(RNG)
If Asc(Mid(RNG.Text, X, 1).Value) > 47 And _
Asc(Mid(RNG.Text, X, 1).Value) < 58 Then
RSLT = RSLT & Mid(RNG.Text, X, 1)
End If
Next
NFW = RSLT
End Function
--
Thanx in advance,
Best Regards,
Faraz
Microsoft Excel
(1)
StringRSLT
(1)
RemoveNonDigits
(1)
CreateObject
(1)
RemAlpha
(1)
CDbl
(1)
StripNumbers
(1)
Vbscript.regexp
(1)
Mike H replied to Faraz A. Qureshi
Hi,
Try it like this
Function nfw(rng As Range)
Dim RSLT As String
RSLT = ""
For X = 1 To Len(rng.Value)
If Asc(Mid(rng.Value, X, 1)) > 47 _
And Asc(Mid(rng.Value, X, 1)) < 58 Then
RSLT = RSLT & Mid(rng.Value, X, 1)
End If
Next
nfw = RSLT
End Function
--
Mike
When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
Jacob Skaria replied to Faraz A. Qureshi
Hi Faraz
---In your code .Value should be removed as below
Function NFW(RNG As Range)
Dim RSLT As String
RSLT = ""
For x = 1 To Len(RNG)
If Asc(Mid(RNG.Text, x, 1)) > 47 And _
Asc(Mid(RNG.Text, x, 1)) < 58 Then
RSLT = RSLT & Mid(RNG.Text, x, 1)
End If
Next
NFW = RSLT
End Function
--OR to make it short
Function NFW1(RNG As Range) As String
Dim intX As Integer
For intX = 1 To Len(RNG)
If IsNumeric(Mid(RNG, intX, 1)) Then NFW1 = NFW1 & Mid(RNG, intX, 1)
Next
End Function
--
Jacob
Peter T replied to Faraz A. Qureshi
In addition to the advice already given it'd be more efficient to read the
value of the range to a string variable first rather than reading the range
in each loop.
FWIW if you have very long strings there are faster ways without using the
various string functions like Asc and Mid.
Other things to consider
- maybe declare the input argument As Variant,
- how to cater for the input of a multi-cell range
- is a potential decimal separator relevant.
Regards,
Peter T
Gord Dibben replied to Faraz A. Qureshi
Another addition to the mix.
Function RemAlpha(str As String) As String
'Remove Alphas from a string
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\D"
RemAlpha = re.Replace(str, "")
End Function
Gord Dibben MS Excel MVP
ryguy7272 replied to Gord Dibben
Yeap, be mindful of the .Value and the .Text.
This is another way to do what you want to do:
Function numit2(r As Range) As String
Dim s As String, s2 As String, c As String
s2 = ""
s = r.Value
l = Len(s)
For i = 1 To l
c = Mid(s, i, 1)
If c Like "#" Then
s2 = s2 & c
End If
Next
numit2 = s2
End Function
Public Function stripNumbers(rng As Range)
Dim i As Integer
For i = 1 To Len(rng.Value)
If Mid(rng.Value, i, 1) >= "0" And Mid(rng.Value, i, 1) <= "9" Then
strNum = strNum & Mid(rng.Value, i, 1)
End If
Next
stripNumbers = CDbl(strNum)
End Function
--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.
Rick Rothstein replied to Faraz A. Qureshi
One more method to consider...
Function RemoveNonDigits(ByVal StrIn As String) As String
Dim X As Long
For X = 1 To Len(StrIn)
If Not Mid(StrIn, X, 1) Like "#" Then Mid(StrIn, X, 1) = " "
Next
RemoveNonDigits = Replace(StrIn, " ", "")
End Function
--
Rick (MVP - Excel)
Faraz A. Qureshi replied to Faraz A. Qureshi
WOW!!!
Sure am lucky 2 have a collection of priceless friends like u all!!!
Thanx guys!
--
Thanx in advance,
Best Regards,
Faraz
Win7 VirtualStore Weirdness with Excel 2002 and earlier Excel I thought I'd share a weird experience I had today in case it saves installer for it - - which registers it as an add-in for all installed versions of Excel - - and tested it. Fine in Windows 2000 in all installed versions of Excel. Boot to Windows XP, fine there too. Boot to Windows 7. Fine in Excel 2010, 2007, and 2003. But Excel 2002 and 2000 were still loading the old version of the add-in. I uninstalled the add-in, made sure it was gone, and reinstalled it. Same problem in Excel 2002 and earlier; still works fine in Excel 2003 and later. I opened the VB editor in Excel 2003 and in Excel 2002
Aggiornamento - Microsoft Risponde allevoluzione delle Community Excel Cosa accade? Per informarvi che Microsoft comincerà presto a chiudere I newsgroup e a “transitare”, pian piano, se lo vorrete, il traffico sui Forum Microsoft. Perchè? Come ben sapete, i newsgroup sono esistiti per molti anni; il problema però è newsgroup. E’ possibile trovare informazioni su come installare e configurare il bridge qui: http: / / connect.microsoft.com / MicrosoftForums / Perchè i newsgroup verranno chiusi? Tutti i newsgroup pubblici verranno chiusi tra il la chiusura dei newsgroup, dove posso andare online per continuare le discussioni e ricevere supporto? Microsoft vi fornisce un’ampia scelta di forum, alcuni dei quali coprono lo stesso argomento od ottimale per continuare le discussioni online. Raccomandiamo quindi di cominciare con il http: / / social.answers.microsoft.com / Forums / it-IT / officeexcelit forum Vi invitiamo comunque a visitare i Forum Microsoft: http: / / www.microsoft.com / communities / forums / default.mspx Se ho domande, chi devo contattare? Se
Erinnerung - Microsoft reagiert auf Evolution der Community Excel Microsoft Office betrachtet Community als Teil des neuen Office 2010 Wie Sie wissen ist das Jahr 2010 ein wichtiges Jahr f?r Microsoft. Die neue Office-Version kommt auf den Markt. Office 2010 soll Ihnen das Leben und Hersteller beim Kauf eines neuen PCs erwerben. Ihre gew?nschte Version ist auch direkt von Microsoft erh?ltlich. Durch eine neue Methode zur Softwarebereitstellung mit dem Namen ?Click-To-Run? gelingt individuell! Worum geht es? Diese Nachricht dient dem Zweck, Sie dar?ber zu informieren, dass Microsoft demn?chst die Newsgroups abstellen und die Nutzer auf die Microsoft Foren verweisen wird. Warum? Wie Sie vielleicht wissen, gibt es Newsgroups schon seit einigen Jahren doch ist die Nutzung der Microsoft Newsgroups seit Jahren stetig zur?ckgegangen, da Kunden und Teilnehmer vermehrt L?sungen in den
Contatore Excel Ciao, sto cercando di fare in modo che all'apertura di un file venga scritta che contenga le date di apertura. Qualcuno ha uno script gi = E0 fatto? tks Sandro Excel - Italian Discussions ThisWorkbook.Worksheets Foglio1 With sh (1) Nothing End Sub Modifica Foglio1 (1) Worksheets (1) Excel 2010 (1) Excel 2007 (1) Macro (1) Date (1) VBA (1) = 3D = 3D = 3D = 3D = 3D = 3D = 3D le cose *fatte bene*. Questo nel modulo di codice di ThisWorkbook (o Questa_Cartella_Di_Lavoro se utilizzi Excel 2010): Private Sub Workbook_Open() Dim sh As Worksheet Set sh = ThisWorkbook.Worksheets("Foglio1") With sh il nome del foglio, no? Altrimenti, 'ndo cojo cojo(che i romani mi scusino). Ed Excel 2007 e 2010 hanno oltre un milione di righe, non 65536. - -- -- -- -- -- -- -- -- -- -- -- -- -- Mauro Gamberini Microsoft MVP - Excel http: / / www.riolab.org / http: / / www.maurogsc.eu / On 21 Dic, 14:13, "Mauro Gamberini
Statistiche newsgroup m.p.i.o.e. Excel Per la prima volta da Luglio 2003, questo mese i post su *nostro* ng si attesteranno sotto a quota 1.000. Ricordo che qui: http: / / groups.google.it / group / microsoft.public.it.office.excel / about potete trovare le statistiche relative al *nostro* ng. Il calo del traffico è *male i *cugini* Access e Word e per il *papà* Office: http: / / groups.google.it / group / microsoft.public.it.office.access / about http: / / groups.google.it / group / microsoft.public.it.office.word / about http: / / groups.google.it / group / microsoft.public.it.office / about - - - -- -- -- -- -- -- -- -- -- -- -- -- -- Mauro Gamberini Microsoft MVP Excel http: / / www.riolab.org / http: / / www.maurogsc.eu / http: / / social.answers.microsoft.com / Forums / it