三角形的VBA区域

时间:2019-03-02 09:25:58

标签: excel vba

我是VBA的新手,因此无法正常工作。有任何想法吗?这是一项作业,因此必须采用这种格式,因此我在“显示区域”中不断收到错误消息。

Private Sub cmbOK_Click()
'Declare variables
Dim TriBase As Single
Dim TriHeight As Single
Dim TriArea As Single
'Set the variables to the values in the textboxes
TriBase = Val(txtBase.Text)
TriHeight = Val(txtHeight.Text)
'Calculate area
TriArea = (TriBase * TriHeight) * 0.5
'Display area
lblArea.Text = Str(TriArea)

1 个答案:

答案 0 :(得分:1)

Label的可见部分是其Caption
您可能还希望将显示的小数缩短Format
如果计算精度必须更高,请从Single更改为Double

Private Sub cmbOK_Click()
    'Declare variables
    Dim TriBase As Single
    Dim TriHeight As Single
    Dim TriArea As Single
    'Set the variables to the values in the textboxes
    TriBase = Val(txtBase.Text)
    TriHeight = Val(txtHeight.Text)
    'Calculate area
    TriArea = TriBase * TriHeight * 0.5
    'Display area
    lblArea.Caption = Format(TriArea, "0.00")
End Sub