从VBA中带有变量的文本框中获取值
在我的项目中,我尝试使用一个函数,该函数将从任何控件返回文本或值。我有实际形式的代码从VBA中带有变量的文本框中获取值,vba,textbox,null,Vba,Textbox,Null,在我的项目中,我尝试使用一个函数,该函数将从任何控件返回文本或值。我有实际形式的代码 Function GetVar(whatbox As Control) As Variant 'get value from textbox or checkbox whatbox.SetFocus If whatbox.ControlType = acTextBox Then GetVar = whatbox.Text Else GetVar =
Function GetVar(whatbox As Control) As Variant
'get value from textbox or checkbox
whatbox.SetFocus
If whatbox.ControlType = acTextBox Then
GetVar = whatbox.Text
Else
GetVar = whatbox.Value
End If
End Function
现在,我这样称呼它:
vIssuesPerMonth = GetVar(Me.NumberofCopies)
vCostIssuePrint = GetVar(Me.CostIssuePrint)
vCostIssueMail = GetVar(Me.CostIssueMail)
vSurcharge = GetVar(Me.Surcharge)
vSurchargeNonMember = GetVar(Me.SurchargeNonMember)
然后我用这些值做一些数学运算
vAvCostPerPrint = vCostIssuePrint / vIssuesPerMonth
vAvCostPerMail = vCostIssueMail / vIssuesPerMonth
我的问题是,GetVar
不起作用,它返回“”,因此计算失败,出现“类型不匹配”错误
如果我在调试期间查看这些值,它会说whatbox
为null,即使我已经发送了正确的控件名称。似乎未正确传递me.controlname
我做错了什么?在哪种情况下返回的是
“
?当它不是文本框
或总是?该函数似乎总是使用if
而不是else
触发。在调试器中,值为空-即没有值。我无法判断它是否返回“”或null。这是文本框。我必须搜索组合框或其他东西的示例,但现在我想求解文本框。似乎Me.controlname
未正确获取。您是否尝试返回CDbl(whatbox.Text)?设置焦点的目的是什么?