检查传递的变量

时间:2016-06-02 05:53:03

标签: vba excel-vba excel

是否可以检查函数中传递的变量是否是特定文本?例如,我有一个功能:

Function dateCheck2(dateValue As String) As Boolean

If IIf(IsDate(frmResponse.txtEndDate), Format(CDate(frmResponse.txtEndDate), _
    "mm\/dd\/yyyy"), "") = dateValue Then
    dateCheck2 = True
    Exit Function
End If

End Function

然后我必须在UForm中调用它,让我们说:

dateCheck2(sample)

我想要检查传递的变量是否为sample。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

或者您可以创建自己的类(在VBA中称为数据类型),它将包含变量名称和值。像这样的东西

Public Type myFluffyType
     fluffyName as string
     fluffyValue as string
end Type

然后你可以像这样调暗你的变量

Dim fluffyVariable as myFluffyType

并初始化

fluffyVariable.fluffyName = "fluffyVariable"
fluffyVariable.fluffyValue = "notSoMuchFluffyValue"

然后当你将它传递给你的函数时,你就拥有了所需的所有信息

编辑:关于你的问题你可以做这样的事情。但我不确定你接下来会做什么

Public type myType
     varName as string
     varValue as string
end type


Sub callDateCheck2
   Dim sample as myType
   sample.varName = "sample"
   sample.varValue = "whateverValues" 
   Call dateCheck2(sample)
end sub


Function dateCheck2(dateValue As myType) As Boolean

if dateValue.varName = "sample" then
    msgbox "everything ok"
else
    msgbox "bad variable name"
    end function
end if

If IIf(IsDate(frmResponse.txtEndDate), Format(CDate(frmResponse.txtEndDate), _
    "mm\/dd\/yyyy"), "") = dateValue Then
    dateCheck2 = True
    Exit Function
End If

End Function