-1

I have a string , from which i want to chech whether each element is an alphabet a number a "." or a "/" and print only alphabets of the string?

Community
  • 1
  • 1
saif
  • 27
  • 4

1 Answers1

1

This rather inelegant combination of two functions would do it:

Function strip_non_alpha_words(sentence As String) As String
Dim wrd_to_check As String
For Each wrd In Split(sentence, " ")
    wrd_to_check = wrd
    If wrd_to_check = alpha_only(wrd_to_check) Then
        strip_non_alpha_words = strip_non_alpha_words & wrd_to_check & " "
    End If
Next
strip_non_alpha_words = Trim(strip_non_alpha_words)
End Function

Function alpha_only(mixedStr As String) As String
Dim ltr As Long, ascii_code As Long
For ltr = 1 To Len(mixedStr)
    ascii_code = Asc(UCase(Mid(mixedStr, ltr, 1)))
    If (ascii_code > 64 And ascii_code <= 90) Then
        alpha_only = alpha_only & Mid(mixedStr, ltr, 1)
    End If
Next
End Function

You can use this in a formula (UDF) like so:

=strip_non_alpha_words(A1)
CLR
  • 11,284
  • 1
  • 11
  • 29
  • Can u please modify the code for input Firefox 1.0b2pre. Output is Firefox. In other words it should remove combination of alphabet ,number and period. Thanks in advance – saif Mar 06 '17 at 10:39
  • Also is it possible to do with excel formulas? – saif Mar 06 '17 at 10:40
  • So you're actually after something that examines each 'word' within a 'sentence' and discards those that contain anything other than the alphabetical strings? – CLR Mar 06 '17 at 10:50
  • Exactly. Thanks in advance. – saif Mar 06 '17 at 11:00
  • It's possible but very messy to do these with a formula - see this for example http://stackoverflow.com/questions/42507062/count-words-in-cell-dont-count-html-tags/42565345#42565345 – Tom Sharpe Mar 06 '17 at 11:11
  • Ah, okay. I'll alter my answer. – CLR Mar 06 '17 at 11:27
  • CLR the code is not working for 3 inputs. Please help for Adobe AIR (Unknown Version), Nokia S40 Browser 2.2.0, UP.Browser – saif Mar 06 '17 at 12:16
  • It looks like it's working for me: I get `Adobe AIR` (removing two words with a `( or )` attached.. `Nokia Browser` removing the parts with numbers and the word `UP.Browser` has a `.` so is ignored. – CLR Mar 06 '17 at 12:20
  • CLR the code is not working for 3 inputs. Please help for Adobe AIR (Unknown Version), Nokia S40 Browser 2.2.0, UP.Browser I want outputs as Adobe AIR (Unknown Version), Nokia S40 Browser, UP Browser. Thanks in advance – saif Mar 06 '17 at 12:23
  • That contradicts your original request @Saif . The `S40` in the Nokia item for instance contains numeric characters `4` and `0`. By the rules you laid out this should be ignored. – CLR Mar 06 '17 at 12:31
  • You can't include the `S40` of `Nokia S40 Browser` but exclude the `1.0b2pre` of `Firefox 1.0b2pre` unless you rewrite the rule to 'Ignore words that contain a `.`' but if you did that, you'd still exclude `UP.Browser`. – CLR Mar 06 '17 at 12:36
  • Thanks a lot for the suggestion. I will simplify my input data – saif Mar 06 '17 at 12:50
  • Can you please explain why you have used UCase function in "ascii_code = Asc(UCase(Mid(mixedStr, ltr, 1)))" line ? – saif Mar 08 '17 at 09:33