Вопрос

Here's my code but i want to know how will I know if the string has special characters like '/' or ':'.Many Thanks. Much great if you can edit my function.

Do Until EOF(1)
   Line Input #1, LineFromFile <-----LineFromFile is the string
     If HasCharacter(LineFromFile) = True Then
        MsgBox "This File should be uploaded to FilePath2"
      Else
     Blah Blah Blah.......

This is my function

Function HasCharacter(strData As String) As Boolean
   Dim iCounter As Integer

   For iCounter = 1 To Len(strData)
     If ....(Don't know what to say) Then
       HasCharacter = True
       Exit Function
     End If
   Next iCounter
End Function
Это было полезно?

Решение

Change your code to this:

Function HasCharacter(strData As String) As Boolean

     If InStr(strData, "/") > 0 Or InStr(strData, ":") > 0 Then
       HasCharacter = True
     Else
       HasCharacter = False
     End If
End Function

The function InStr returns the position of the string if found, else it returns 0.

Другие советы

You can simply:

if strData like "*[:/]*" then 
    msgbox "This File should be uploaded to FilePath2"
else
    ...

Use InStr(stringToCheck, characterToFind)

Function HasCharacter(strData As String) As Boolean
  If InStr(strData, "/") + InStr(strData, ":") > 0 Then
    HasCharacter = True
  End If
End Function

InStr returns 0 if the character cannot be found in the string. In this case, I add the positions of both special characters together. If the sum of these positions is greater than 0, we know that it contains at least one special character. You can separate this logic if you'd like.

if you have multiple characters then can also invert the checking and its easier to edit than multiple or statements

Function HasCharacter(strData As String) As Boolean
   Dim iCounter As Integer

  For iCounter = 1 To Len(strData)
    If Instr ("/:", Mid (strData, iCounter, 1)) > 0 Then
      HasCharacter = True
      Exit Function
    End If
  Next iCounter

End Function

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top