Chelsea-fc
Chelsea-fc

Reputation: 165

basic syntax error in visual basic excel

I want to Remove Non-Alphanumeric Characters with this Macro.

this simple program gives me a syntax error.

I don't understand this error at all.

Sub CleanAll()
Dim rng As Range
Application.ScreenUpdating = False ' Turns off screen updating
On Error Resume Next ' Macro will continue if it encounters #N/A
'Adjust "Sheet1" and Range in Line 6
For Each rng In Sheets("Sheet1").Range("A2:A1629").Cells
    rng.Value = AlphaNumericOnly(rng.Value) ' Function call
Next
Application.ScreenUpdating = True
End Sub

Function AlphaNumericOnly(strSource As String) As String
Dim i As Integer
Dim strResult As String
On Error Resume Next
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
'The numbers below match Char Codes to keep
 Case 48 To 57, 65 To 90, 97 To 122:
    strResult = strResult & Mid(strSource, i, 1)
End Select
Next
AlphaNumericOnly = strResult
End Function

Upvotes: 0

Views: 1109

Answers (1)

rory.ap
rory.ap

Reputation: 35260

You have & which is not valid syntax. Change it to this:

strResult = strResult & Mid(strSource, i, 1)

Upvotes: 1

Related Questions