Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Userform Textboxes Subtraction

0

G'day All @ Teachexcel; It's been a while since I asked for some assistance on excel Userform.

I've had a brain block out and could not make it work and couldn't find that much information either regarding subtraction in VBA on the web.

This is a simple Subtraction of two textboxes and return the answer to a third textbox; I never had the opportunity to exercise subtraction in the past unfortunately.

As much as I tried to manipulate the VBA out couldn't come with a satisfying result.

I just wondering if someone please Teachexcel forum could throw some light on the subject.

Thnks Kindly

Answer
Discuss

Discussion

Jd. Please see Revision 26 March 2024 to my Answer and file (with demo)
John_Ru (rep: 6142) Mar 26, '24 at 3:33 am
Add to Discussion

Answers

0

Hi jdgrapes (Joseph?) and good evening (from here)!

Your question doesn't say which procedure you had trouble with but I think the answer is the same. Textboxes store strings (even in the Value property) so you need to convert them to an appropriate data type.

For example, you have a procedure in your UserForm as follows:

Private Sub txtTTl02_Change()
    Me.txtTTL02.Value = Val(Me.txtTrgt3.Value) - Val(Me.txtTTL01.Value)

End Sub

but the Val function doesn't exist in VBA (unless you define it). 

I suggest you use a VBA conversion function like Cdbl (which can convert a number-like string into a Double value which can be manipulated arithmetically). That would change the above code to read:

Private Sub txtTTl02_Change()
    Me.txtTTL02.Value = CDbl(Me.txtTrgt3.Value) - CDbl(Me.txtTTL01.Value)

End Sub

and the control txtTTL02 would show the result of the subtraction.

The Microsoft guidance on various datatype conversions is here: [LINK URL="learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/type-conversion-functions"]Type conversion functions[/LINK]

Revision 26 March 2024

The attached revisied file has an orange button "Add textboxes demo". Click that and a new userform appears. Type two number is the left hand text boxes then click the green button- that runs this code:

Private Sub Cb_Add_Click()

    ' check text can be converted to numbers
    If Not IsNumeric(TextBox1) Or Not IsNumeric(TextBox1) Then Exit Sub
    ' convert to Double data type and add values
    TextBox3.Value = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
    ' format to fix 2 decimal places
    TextBox3.Text = Format(TextBox3.Value, "#,##0.00")

End Sub

The result (to 2 dps) will be in the textbox below the green button.

Hope this helps and you can apply to your problem- if so, please be sure to mark this Answer as Selected. If not, please clarify your original question.

p.s. Next time, please try to be more specific when asking a question- you're more likely to get a better answer. Also, kindly edit your original question to correct the spelling of Subtraction in the title- that will help others 

Discuss

Discussion

Hi John Ru and G'day to you; Thank you kindly for you chat and having a look at the ‘Textboxes Subtraction’ issue; unfortunately, the code wasn’t successful in this case. I shall try and look a bit deeper.
Many Thanks
best Regards
jdgrapes
jdgrapes (rep: 14) Mar 24, '24 at 8:11 pm
Jd. If you clarify your question I should be able to help e.g.  confirm which textbox /code yiu need to fix. 
John_Ru (rep: 6142) Mar 25, '24 at 3:51 am
Jd. My revised answer /file now demonstrate subtraction of textbox values.
John_Ru (rep: 6142) Mar 27, '24 at 4:34 pm
Guess I wasted my time (again)! :--(
John_Ru (rep: 6142) Mar 28, '24 at 3:56 pm
Still no comment (from a user with a Reputation) so I'm wondering if email notifications have stoped working on this site...
John_Ru (rep: 6142) Apr 18, '24 at 4:53 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login