简体   繁体   English

Excel VBA Userform如何添加和减去变量?

[英]Excel VBA Userform How to add and subtract variables?

I have a Userform, on Page 1 of the Userform the user enters some text into a TextBox called as_1 and another TextBox called annualsaving1. 我有一个Userform,在Userform的第1页上,用户将一些文本输入到名为as_1的TextBox和另一个名为annualsaving1的TextBox中。

On Page 5 of the Userform I would like to add both variables up in Textbox called 'TextBox36'. 在Userform的第5页上,我想在Textbox中添加两个变量,称为“TextBox36”。 This is the code I have been using: 这是我一直在使用的代码:

Private Sub Page5a()
I = as_1 + annualsaving1
TextBox36.Value = I
End Sub

When I use this code, only the value of as_1 appears. 当我使用此代码时,仅显示as_1的值。 It doesn't add 'annualsaving1'. 它没有添加'annualsaving1'。

Any help would be appreciated, Thank you :) 任何帮助将不胜感激,谢谢:)

as_1 and annualsaving1 are of type TextBox . as_1annualsaving1TextBox类型。 To add their values, you need to access their .Value property and convert it to type Integer : 要添加它们的值,您需要访问它们的.Value属性并将其转换为Integer类型:

I = CInt(as_1.value) + CInt(annualsaving1.value)

Be careful though: if user enters something that cannot be parsed as number, your program will crash. 但请注意:如果用户输入的内容无法解析为数字,则程序将崩溃。

try below 试试下面

Private Sub Page5a()
    TextBox36.Text = Val(as_1.Text) + Val(annualsaving1.Text)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM