Avatar of Patrick O'Dea
Patrick O'DeaFlag for Ireland

asked on 

VBA to include only a-z in end string

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"



Microsoft ExcelVB Script

Avatar of undefined
Last Comment
Patrick O'Dea
ASKER CERTIFIED SOLUTION
Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Ardhendu Sarangi
Ardhendu Sarangi
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of david251
david251

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Patrick O'Dea
Patrick O'Dea
Flag of Ireland image

ASKER

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
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

Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of Patrick O'Dea
Patrick O'Dea
Flag of Ireland image

ASKER

Thanks Patrickab,

You code is much appreciated.Points will be shared.
Avatar of Patrick O'Dea
Patrick O'Dea
Flag of Ireland image

ASKER

Thanks all,

Problem solved !
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo