Handling null values #error (1 Viewer)

DavidCon

Registered User.
Local time
Yesterday, 23:12
Joined
Apr 14, 2011
Messages
23
Hi,

Hoping for a helping hand please.

I have this bit of VBA that basically removes unwanted characters from a Text field:

Code:
Public Function StripOutVoidCharacters(varstring As String) As String

 Dim varchar As String, i As Long, finalstring As String

 For i = 1 To Len(varstring)
 varchar = Mid(varstring, i, 1)
 Select Case varchar
 Case "'", "?", "/", "!", "%", "-", " ", ".", "+", "=", "\", "<", ">", ",", "!", "(", ")"
 Case Else
 finalstring = finalstring & varchar
 End Select
 Next i

 StripOutVoidCharacters = finalstring

 End Function

The problem is, if the field is blank it shows as #error

Can you please advise how I can amend the code so that blank text fields stay blank and are not reported as #error?

Thanks for reading
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:12
Joined
May 7, 2009
Messages
19,246
Public Function StripOutVoidCharacters(varstring As Variant) As String

Dim varchar As String, i As Long, finalstring As String
varstring = varstring & ""
For i = 1 To Len(varstring)
 

Users who are viewing this thread

Top Bottom