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)
解决方案
a 的可见部分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
推荐阅读
- python - 如何在pytest中删除带有monkeypatch或mock的库?
- haskell - Haskell 类型同义词可以用作类型构造函数吗?
- firebase - 有没有办法在firebase中将别名设置为项目支持电子邮件?
- android - Android Studio 计数器
- javascript - 如何从 Elementor Widget 控制字段中获取值
- python - Disocrd.py 试图制作一个检测多个单词的不和谐机器人
- ios - 尝试推送通知
- regex - 如何在文档中使用正则表达式对字符串进行完全匹配以进行修改
- ios - “不能调用非函数类型‘字符串’的值?
- android - Android sdkmanager 为任何参数提供 IllegalArgumentException