1

我正在尝试在 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

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

4

1 回答 1

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
于 2012-11-06T12:53:12.480 回答