我在我的小型 VBA 应用程序中使用变体数据类型来在同一函数下使用字符串和整数数据。但是,似乎只有我的整数计算有效,字符串相关部分只给出#value!错误。
在我的应用程序中,我在将单个变量定义为变体的同一函数下同时使用字符串和整数。还采取了措施在“”中指定字符串。
当我使用变体数据类型时,如果我需要以任何其他方式区分 string 和 int ,谁能给我建议?
我在这里分享我的应用程序以供参考,因为由于很多依赖关系,我无法复制这段代码。(表:预测) VBA_App
如果我正确理解,您将传递一个数值以两种格式运行 - 整数和字符串。当然,您可以使用变体,在使用此值之前,请检查它:
IsNumeric(yourVariant) gives you True or False:
IsNumeric("222") - True
IsNumeric(222) - True
IsNumeric("222abc") - False
之后确保您可以将 Variant 转换为 Integer ( http://msdn.microsoft.com/en-us/library/s2dy91zy.aspx ): CInt(yourVariant)
if IsNumeric(yourVariant) then
someIntVariable = CInt(yourVariant)
else
MsgBox "bla bla bla"
EndIf
我查看了一些您的代码,您可以使用 Select Case Statement ( http://msdn.microsoft.com/en-us/library/cy37t14y.aspx ) 或保留 If-Else-If。评论中的讨论可以帮助您选择。
如果我的回答不正确,请提供更具体的代码。我发现很难理解您的应用程序发生了什么。
fcComm
例如,我尝试从单元格 E68、表预测中逐步分析代码运行函数。setFcResult
我们接下来要达到的功能:
Function setFcResult(bettype As String, fcOption)
' bettype = "FT.OU"
' fcOption = "HF.HL"
setFcResult = True
bettype = UCase(bettype)
fcOption = UCase(fcOption)
If bettype = "FT.OU" Then
If fcOption >= 0 Then ' you compare "HF.HL" with 0. It returns true. You can verify this by yourself.
hfs = 5
afs = fcOption - 5 ' here you perform "HF.HL" - 5. It returns error and function terminates.
Else
setFcResult = False
End If
ElseIf bettype = "HT.OU" Then
...
表赔率看起来像:
投注类型搜索:
bettype = Application.WorksheetFunction.VLookup(oddsId, odds.DataBodyRange, 3, False)
您搜索oddsId
,在第一列中VLookup
搜索- 它是 A 列,但在 A 列中您有 TransId。oddsId
odds.DataBodyRange
因此,您的 fcOption 变量的投注类型不正确。