How to make an automatic calculaton in a Userform?

freeasabird

New Member
Joined
Jun 21, 2018
Messages
5
So I have this question that asks: In the past 30 days, how many days did you have:
And the user puts in 3 different amounts of days, mild, moderate, or severe.

I want the final textbox to automatically calculate 30-input for mild-input for moderate-severe.

here is an image:
geiryAz.png
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I am assuming you require the "TextBox1.value" to be the value left from 30 after removing the sum of the 3 comboboxes

Load these codes into your Userform Module.
Code:
Option Explicit
Private [COLOR=navy]Sub[/COLOR] ComboBox1_Change()
nTot
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]

Private [COLOR=navy]Sub[/COLOR] ComboBox2_Change()
nTot
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]

Private [COLOR=navy]Sub[/COLOR] ComboBox3_Change()
nTot
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]

Private [COLOR=navy]Sub[/COLOR] UserForm_Initialize()
'[COLOR=green][B]Use this to load comboboxes with list of Numbers (1 to 30)[/B][/COLOR]
'[COLOR=green][B]unless you already have them numbered. !!!!![/B][/COLOR]
[COLOR=navy]Dim[/COLOR] nList [COLOR=navy]As[/COLOR] Variant
nList = Evaluate("=Row(1:30)")
ComboBox1.List = nList
ComboBox2.List = nList
ComboBox3.List = nList
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
[COLOR=navy]
Sub[/COLOR] nTot()
TextBox1.Value = (30 - (Val(ComboBox1.Value) + Val(ComboBox2.Value) + Val(ComboBox3.Value)))
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Thanks for all of your help MickG

This is how i load the numbers into the combobox:

Code:
Private Sub Load_Numbers()
Dim i As Integer
With UserForm_Headache.ComboBox_Age_First_experienced_Headache
For i = 1 To 99
With UserForm_Headache.cbox_bother_years_headache
.AddItem i
End With
With UserForm_Headache.ComboBox_Age_First_experienced_Headache
.AddItem i
End With
With UserForm_Headache.AgeFirstExpHeadaches
.AddItem i
End With
 
Upvote 0
It wont let me edit my reply :(.

What does nTot do?

So you take the value of the textbox, and you set the output to be 30 - the values of the other textboxes added.

But since I defined i as Integer in my Private Sub Load_Numbers() I dont have to do Val in Val(ComboBox1.Value) since i is already an integer?

Thanks for your help. I am a very big beginner.

If anyone in the future is curious (I know reading past forum comments is helpful to me when I google how to do things), my code is::

Code:
Private Sub Load_Numbers()
Dim i As Integer
With UserForm_Headache.ComboBox_Age_First_experienced_Headache
For i = 1 To 99
With UserForm_Headache.cbox_bother_years_headache
.AddItem i
End With
With UserForm_Headache.ComboBox_Age_First_experienced_Headache
.AddItem i
End With
With UserForm_Headache.AgeFirstExpHeadaches
.AddItem i
End With
 
Upvote 0
You're welcome
Glad you have it working .

Thanks again for your help, just one more thing. When I put in that code it ran it, but it would still only update 30- mild headaches. No matter what I did it wouldnt account for the second or third value.

So i changed the code into this:
Code:
[COLOR=rgba(255, 255, 255, 0.701961)][FONT=Whitney]Private Sub nTot() Dim noheadaches As String Dim noheadaches1 As String Dim noheadaches2 As String noheadaches = (30 - Val(Pastdays_MildHeadaches)) noheadaches1 = (noheadaches - Val(Pastdays_ModerateHeadaches)) noheadaches2 = (noheadaches1 - Val(Pastdays_SevereHeadaches)) txtbox_noheadaches.Value = noheadaches2 End Sub[/FONT][/COLOR]
 
Upvote 0
You're welcome
Glad you have it working .
I changed it to this:

Code:
Private Sub nTot()
Dim noheadaches As String
Dim noheadaches1 As String
Dim noheadaches2 As String
noheadaches = (30 - Val(Pastdays_MildHeadaches))
noheadaches1 = (noheadaches - Val(Pastdays_ModerateHeadaches))
noheadaches2 = (noheadaches1 - Val(Pastdays_SevereHeadaches))
txtbox_noheadaches.Value = noheadaches2
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top