就像在VBA中没有明显原因返回False一样

Mar*_*KPL 1 vba

我试图在VBA中编写一个数据过滤脚本,并认为使用单独的函数这样做是个好主意.因此,我得到以下代码:

Sub checkFormat()
Dim cont As String
cont = "21-345"
cont = funkcja(cont)
check = cont Like "##-###"

Debug.Print check & " vartype: " & VarType(cont)

End Sub

Private Function funkcja(Param1 As String)
If ((Left(Param1, 1) = " ") Or (Right(Param1, 1) = " ")) Then
    Param1 = Trim(Param1)
    Debug.Print "Cut"
Else
    Debug.Print "Nothing to cut"
End If
Debug.Print "Returned: """ & Param1 & """" & " vartype: " & VarType(Param1)
End Function
Run Code Online (Sandbox Code Playgroud)

问题是check变量返回False,无论我分配给它的值是什么.但是,一旦我注释掉该cont=funkcja(cont)行,Like函数就会开始工作.谁能告诉我funkcja函数对字符串做了什么,以便Like返回False?我检查了变量类型,但它始终设置为String ...

Ale*_* K. 5

因为funkcja总是返回"",因为你没有告诉它返回Param1:

Private Function funkcja(Param1 As String) as string '//type it
If ((Left(Param1, 1) = " ") Or (Right(Param1, 1) = " ")) Then
    Param1 = Trim(Param1)
    Debug.Print "Cut"
Else
    Debug.Print "Nothing to cut"
End If
'//
'//set the return value
funkcja = param1 
'//
'//
Debug.Print "Returned: """ & Param1 & """" & " vartype: " & VarType(Param1)
End Function
Run Code Online (Sandbox Code Playgroud)