首页 > 解决方案 > 三角形的 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)

标签: excelvba

解决方案


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

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

推荐阅读