Excel VBA,Null文本框和CInt

时间:2014-12-29 00:37:57

标签: vba excel-vba excel

我要做的是从4个文本框中取出值并将它们放入4个不同的工作表中,但它们必须是整数才能使AverageSum函数起作用。但是,如果值为Null则清除文本框,以便不将值计算为月度总和或平均值。我继续使用Else子句获得类型不匹配错误,当我将鼠标悬停在它上面时,它会显示frmDataEntry.tbBloodDraws = ""。我认为else子句只会获取值并将其更改为Int,只要它不为空或Null?我错过了什么?

Private Sub btnOK_Click()

' Get currently selected Cell of the Data Entry Sheet
Dim DataEntryCell As String
DataEntryCell = ActiveCell.Address

'Copy values from the dialog box into the correct sheets

Worksheets("Blood Draws").Activate
Range(DataEntryCell).Select

If (IsNull(frmDataEntry.tbBloodDraws)) Then
    ActiveCell.Value = ActiveCell.Clear
Else
    ActiveCell.Value = CInt(frmDataEntry.tbBloodDraws)
End If

End Sub

1 个答案:

答案 0 :(得分:1)

使用ClearContents仅删除单元格的内容,然后使用IsNumeric检查字符串是否可以解释为数字。

If tbBloodDraws.Text = "" Then
    ActiveCell.ClearContents
ElseIf IsNumeric(tbBloodDraws.Text) Then
    ActiveCell.value = CInt(tbBloodDraws.Text)
End If