Solved

VBA to include only a-z in end string

Posted on 2010-11-08
8
603 Views
Last Modified: 2012-05-10
Hi,
See below.
How do I remove all characters not in the range a-z or A-Z.
Essentially, I am looking for code to replace the line in bold below.

Dim sourcestring , destinationstring as string
sourcestring = "abc./ xyzABC"

**destinationstring = replace(sourcest......????
msgbox destinationstring shows   "abcxyzABC"



0
Comment
Question by:Patrick O'Dea
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 45

Accepted Solution

by:
patrickab earned 167 total points
ID: 34084979
21Dewsbury,

The code below is in the attached file.

Usage:

=bb(A1)

where A1 contains the string.

Patrick
Function bb(ByVal target As Range) As String
Dim i As Long
Dim str1 As String

For i = 1 To Len(target)
    Select Case Asc(Mid(target, i, 1))
        Case 65 To 90
           str1 = str1 & Mid(target, i, 1)
        Case 97 To 122
            str1 = str1 & Mid(target, i, 1)
    End Select
Next i

bb = str1

End Function

Open in new window

21Dewsbury-01.xls
0
 
LVL 20

Assisted Solution

by:pari123
pari123 earned 167 total points
ID: 34084996
Assuming your data is in Column C, you can use something similar to this -


            For k = 1 To Len(ws2.Range("C" & i))
                If (Asc(Mid(ws2.Range("C" & i), k, 1)) >= 65 And Asc(Mid(ws2.Range("C" & i), k, 1)) <= 90) Or _
                   (Asc(Mid(ws2.Range("C" & i), k, 1)) >= 97 And Asc(Mid(ws2.Range("C" & i), k, 1)) <= 122) Or _
                   (Asc(Mid(ws2.Range("C" & i), k, 1))) = 32 Then
                Else
                    Mid(ws2.Range("C" & i), k, 1) = ""
                End If
            Next k

Open in new window

0
 
LVL 19

Assisted Solution

by:david251
david251 earned 166 total points
ID: 34085239
you can also use this function with regular expressions like this:
Function CharactersOnly(strData As String) As String
    Dim RE As Object, REMatches As Object
    Dim strTemp As String
    strTemp = ""
    
    Set RE = CreateObject("vbscript.regexp")
    With RE
        .MultiLine = False
        .Global = True
        .IgnoreCase = True
        .Pattern = "[A-Za-z]+"
    End With
    
    Set REMatches = RE.Execute(strData)


    For Each i In REMatches
        strTemp = strTemp & i.Value
    Next i
    CharactersOnly = strTemp

End Function

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:Patrick O'Dea
ID: 34085824
pari123,

I chose to use you code (as it suited my limited VBA ability).

However, it crashes at the line;

                    Mid(ws2.Range("C" & i), k, 1) = ""   ;

"Variable required - can't assign to this expression".

Any thought?
I guess it is saying that you can't assign a value to the middle of a string??  Is that right? What would be the syntax to simply replace the character with a "" ??





 For k = 1 To Len(ws2.Range("C" & i))
                If (Asc(Mid(ws2.Range("C" & i), k, 1)) >= 65 And Asc(Mid(ws2.Range("C" & i), k, 1)) <= 90) Or _
                   (Asc(Mid(ws2.Range("C" & i), k, 1)) >= 97 And Asc(Mid(ws2.Range("C" & i), k, 1)) <= 122) Or _
                   (Asc(Mid(ws2.Range("C" & i), k, 1))) = 32 Then
                Else
                    Mid(ws2.Range("C" & i), k, 1) = ""   
                End If
            Next k
0
 
LVL 20

Expert Comment

by:pari123
ID: 34085956
Hi Dewsbury,

Can you please try this code -

regards,
Ardhendu
Sub cleanup()
strX = ""
For i = 1 To Cells(65536, "C").End(xlUp).Row
    For k = 1 To Len(Range("C" & i))
        'MsgBox (Mid(Range("C" & i), k, 1))
        If (Asc(Mid(Range("C" & i), k, 1)) >= 65 And Asc(Mid(Range("C" & i), k, 1)) <= 90) Or _
           (Asc(Mid(Range("C" & i), k, 1)) >= 97 And Asc(Mid(Range("C" & i), k, 1)) <= 122) Then
           strX = strX & Mid(Range("C" & i), k, 1)
        End If
    Next k
    Range("C" & i) = strX
    strX = ""
Next
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:patrickab
ID: 34086019
21Dewsbury,

I would have thought that having provided the VBA code, an example and a file illustrating how it works my very simple function 'bb' would be the easiest method to adopt.

To help I have commented the code below.

Patrick


Function bb(ByVal target As Range) As String
'declare variables
Dim i As Long
Dim str1 As String

'work through the 'target' string, character by character
For i = 1 To Len(target)
'convert each character to its Ascii value
    Select Case Asc(Mid(target, i, 1))
'if the Ascii value is between 65 and 90 (A-Z) then add it to the string str1
        Case 65 To 90
           str1 = str1 & Mid(target, i, 1)
'if the Ascii value is between 97 and 122 (a-z) then add it to the string str1
        Case 97 To 122
            str1 = str1 & Mid(target, i, 1)
    End Select
Next i
'make bb equal to str1
bb = str1

End Function

Open in new window

0
 

Author Comment

by:Patrick O'Dea
ID: 34086737
Thanks Patrickab,

You code is much appreciated.Points will be shared.
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 34088268
Thanks all,

Problem solved !
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question