PDA

View Full Version : VBA check if a string only has you type of character



Movian
08-19-2011, 11:37 AM
Hi,
I am attempting to update a script that pulls in a comma delimited file of sorts.

One of the things that the system does is check for empty lines for spacing. However recently some of the produced files instead if having an empty line will have a variable number of commas on it ",,," or ",,,,,,"

What i need is a quick and easy way to determine if the line has any data OTHER than a , . I have written up a couple functions that check each character on the line and if there is something other than a , it throws it back however it was a little cumbersome.... is there another method that i am not aware of.

hansup
08-19-2011, 11:49 AM
If I understand this correctly, I think it would be pretty simple to use Like to check whether the string contains anything other than a comma.

? ",,,,,," Like "*[!,]*"
False
? "A,,,,," Like "*[!,]*"
True

If I didn't get the character list and pattern quite right, check the help topic for the Like Operator. I think you should be able to come up which a pattern which works.

Paul_Hossler
08-19-2011, 01:35 PM
one way


Sub drv()
Dim s As String
s = "ABC,DEF,GHI"
If Len(Trim(Replace(s, ",", vbNullString))) = 0 Then
MsgBox "Just commas"
Else
MsgBox "More that just commas"
End If

s = ",,,,,,,,,,,,,,,,,,,,,"
If Len(Trim(Replace(s, ",", vbNullString))) = 0 Then
MsgBox "Just commas"
Else
MsgBox "More that just commas"
End If
End Sub


Paul