我正在尝试在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?我检查了变量类型,但它总是设置为String ...
答案 0 :(得分: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