简体   繁体   中英

Nested if statement within a for loop

I am modifying a User Defined Function which I wrote. It removes special characters from a cell (I have posted about this same function a handful of times, as I keep expanding it and learning more about the capabilites of VBA).

What I am trying to do now is add a MsgBox which pops up and tells the user exactley which special characters have been removed. I think that I can do this by using an If statement nested within my existing for loop, like so:

Function removeSpecial(sInput As String) As String
    Dim sSpecialChars As String
    Dim i As Long
    Dim sReplaced As String
    sSpecialChars = "\/:*?™""®<>|.&@# %(_+`©~);-+=^$!,'" 'This is your list of characters to be removed
    For i = 1 To Len(sSpecialChars)
        sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
        If (sInput = sSpecialChars) Then 'If statement to check when a character has been removed
        sReplaced = sInput 'String variable to store each special character which was replaced
    Next
    MsgBox sReplaced & " These were removed"
    sInput = UCase(sInput)
    removeSpecial = sInput

End Function 'end of function

Currently this throws me into an infinate loop and I have to force close Excel. What I was trying to do with the above code was to check to see if an individual character located at whatever index the Mid function is currently looking at, and then save that character, if replaced, to the String sReplaced. Clearly though, I am in over my head.

Thanks you for the help.

Please examine this version:

Function removeSpecial(sInput As String) As String

    Dim sSpecialChars As String, sTemp As String
    Dim i As Long
    Dim sReplaced As String
    sTemp = sInput
    sSpecialChars = "\/:*?™""®<>|.&@# %(_+`©~);-+=^$!,'"

    For i = 1 To Len(sSpecialChars)
        ch = Mid(sSpecialChars, i, 1)
        If InStr(sTemp, ch) > 0 Then
            sTemp = Replace$(sTemp, ch, "")
            sReplaced = sReplaced & ch
        End If
    Next

    MsgBox sReplaced & " These were removed"
    sTemp = UCase(sTemp)
    removeSpecial = sTemp
End Function

Note: This will not attempt to modify the input, only return a "cleaned-up" output string.

Try this one:

Function removeSpecial(sInput As String) As String
    Dim sSpecialChars As String
    Dim i As Long
    Dim sReplaced As String
    Dim ln As Integer

    sSpecialChars = "\/:*?™""®<>|.&@# %(_+`©~);-+=^$!,'" 'This is your list of characters to be removed
    For i = 1 To Len(sSpecialChars)
        ln = Len(sInput)
        sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
        If ln <> Len(sInput) Then sReplaced = sReplaced & Mid$(sSpecialChars, i, 1)
    Next
    MsgBox sReplaced & " These were removed"
    sInput = UCase(sInput)
    removeSpecial = sInput

End Function 'end of function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM