作者areric ()
看板Visual_Basic
标题[VBA ] 溢位问题
时间Wed May 12 12:24:43 2021
请教各位大大
目前想写个简易计算excel数据的程式
有四个textbox
textbox 1~3(a、b、c)分别显示从excel抓取的数据或自行打上数据
textbox 4(d) 则计算textbox 1~3的数据後显示出来
我的写法是这样:
当a出现数据後 d显示计算结果
Private Sub a_Change()
d.Text = Val(a.Text) * Val(b.Text) / Val(c.Text)
End Sub
结果出现 执行阶段错误'6':溢位
我刚学VBA还请版上大大指导
感谢!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.124.166.109 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Visual_Basic/M.1620793485.A.69A.html
1F:推 LPH66: 因为 c 没有值, Val 之後变成 0 了 05/12 21:07
2F:→ areric: 感谢大大 终於找到问题了 那除了预先在格子内填入数值外 05/13 09:17
3F:→ areric: 还有其他方法吗? 谢谢 05/13 09:17
4F:→ MOONRAKER: 你有听过if吗 05/13 09:51
5F:→ waiter337: Private Sub a_Change() 05/14 11:10
6F:→ waiter337: call cal 05/14 11:11
7F:→ waiter337: end sub 05/14 11:11
8F:→ waiter337: sub cal() 05/14 11:11
9F:→ waiter337: if a.Text="" or a.Text = 0 then exit sub 05/14 11:12
10F:→ waiter337: if b.Text="" or b.Text = 0 then exit sub 05/14 11:12
11F:→ waiter337: if c.Text="" or c.Text = 0 then exit sub 05/14 11:13
12F:→ waiter337: d.Text = Val(a.Text) * Val(b.Text) / Val(c.Text) 05/14 11:13
13F:→ waiter337: end sub 05/14 11:13
14F:→ waiter337: 另外textbox的IMEmode 要改成8 05/14 11:14
15F:→ waiter337: 如果想全靠键盘操作 05/14 11:15
16F:→ waiter337: 可以把前三行取消 05/14 11:15
17F:→ waiter337: 改用 05/14 11:16
18F:→ waiter337: Private Sub a_KeyDown(ByVal KeyCode As MSFors.Retur 05/14 11:17
19F:→ waiter337: nInteger, ByVal Shift As Integer) 05/14 11:17
20F:→ waiter337: If KeyCode = 13 Or KeyCode = 9 Then 05/14 11:17
21F:→ waiter337: Application.EnableEvents = False 05/14 11:17
22F:→ waiter337: Call cal 05/14 11:17
23F:→ waiter337: KeyCode = 0 05/14 11:18
24F:→ waiter337: b.SetFocus 05/14 11:18
25F:→ waiter337: Application.EnableEvents = True 05/14 11:18
26F:→ waiter337: End If 05/14 11:18
27F:→ waiter337: End Sub\ 05/14 11:18
28F:→ waiter337: \ 删掉 不小心按到 05/14 11:19