简体   繁体   中英

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.

On Page 5 of the Userform I would like to add both variables up in Textbox called '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. It doesn't add 'annualsaving1'.

Any help would be appreciated, Thank you :)

as_1 and annualsaving1 are of type TextBox . To add their values, you need to access their .Value property and convert it to type 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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