编译VBA代码时出现补丁错误

时间:2016-03-16 12:07:55

标签: vba

Sub tem()
Function removeSpecial(sInput As String) As String
    Dim sSpecialChars As String
    Dim i As Long
    sSpecialChars = "\/:*?""<>|"
    For i = 1 To Len(sSpecialChars)
        sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), " ")
    Next
    removeSpecial = sInput
End Function

End Sub

1 个答案:

答案 0 :(得分:2)

您不能在Sub中放置函数声明。这里是重新格式化的代码,并且Sub / End Sub行被注释掉了。

'Sub tem()

Function removeSpecial(sInput As String) As String
  Dim sSpecialChars As String
  Dim i As Long
  sSpecialChars = "/:*?""<>|" 
  For i = 1 To Len(sSpecialChars)
    sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), " ")
  Next
  removeSpecial = sInput
End Function

'End Sub

当它从即时窗口运行时:

?removespecial("a/a:a*a?a""a<a>a|a")
a a a a a a a a a