Vous êtes sur la page 1sur 5

Calculations in a Userform – Excel VBA

Contents [show]

In this tutorial I’ll be showing you how to perform multiple calculations in a Userform. We will be using formulas in a
worksheet to do the major work for us. I have used this process successfully in larger applications where multiple
calculations need to be performed in the Userform before the data is sent to the dataset.

Download the Template for Calculations in a Userform


I would recommend that you download the template because it will make it easier for you to complete this tutorial.
The references below are designed for this template. So save time and heartache – use the template. After you
have completed the tutorial then set up your own template and modify the tutorial to suit your own needs and your
calculations in a Userform.

Template Calculations in a Userform

Video: Calculations in a Userform

This video is designed to help you understand how the application will work. It will also give you step-by-step
instructions on creating the Userform and where you should add the code that you will find below.

Process as shown in the above video


The first thing we need to do is to add our named range. You can use a dynamic named range if you wish, which is a
little more difficult to create then the static range below.
If you wish to create a dynamic named range then here is a tutorial along with a sample file that you can download

1
that will get you started with dynamic named ranges?
Dynamic Named Range Tutorial
Adding the named ranges
You should call the named range “Products”
=Sheet1!$I$5:$I$20

Adding the Worksheet formulas


Note: Select the cell and paste the formula in to the formula bar.

Formula for Cell O5


=VLOOKUP(M5,I5:K20,2,0)

Formula for Cell P5


=VLOOKUP(M5,I5:K20,3,0)
Formula for Cell Q5
=(N5*O5)*P5
Formula for Cell R5
=(N5*O5)+Q5

Creating the Userform

2
Make sure that you have watched the video and create the Userform as shown in the illustration above. Notice the
names for the textboxes and command buttons and combobox.
You will need to use these exact names for the code that you will be later adding to the Userform to work.
This Userform thus consists of

7 labels
5 text boxes
2 command buttons
1 ComboBox

Properties for the Userform


There are a couple of things that you can do with the Userform that will make it user friendly.
1. Name it appropriately (does not matter what name you choose but something like frmProducts would be suitable.
2. Add a caption to describe what action this Userform will be achieving.

The ComboBox Rowsource Property

3
In the Properties for the ComboBox click into Rowsource and add the name for the named range that you created
earlier “Products”

Adding the VBA code to the Userform


Here is the code for the Userform. Double click inside the Userform and then copy the code below and paste into the
Visual Basic editor. If you have not already done so make sure you watch the video to get a brief understanding of
what this code is doing.
Option Explicit
'This is the code to send all of the data to the worksheet.
' Notice the conditional If statement at the beginning to guarantee the values have been added.

Private Sub cmdAdd_Click()


Dim nextrow As Range
'check for values
If Me.cmdProducts.Value = "" Or Me.txtQuantity.Value = "" Then
MsgBox "Please add the product and quantity"
Exit Sub
End If
'find the next blank row
Set nextrow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
'send the data to the database
With nextrow
.Value = Me.cmdProducts.Value
.Offset(0, 1).Value = Me.txtQuantity.Value
.Offset(0, 2).Value = Me.txtKg.Value
.Offset(0, 3).Value = Me.txtPercent.Value
.Offset(0, 4).Value = Me.txtMarkup.Value
.Offset(0, 5).Value = Me.txtTotal.Value
End With
'update the worksheet
Application.ScreenUpdating = True
'clear the values
With Me
.cmdProducts.Value = ""
.txtQuantity.Value = ""
.txtKg.Value = ""
.txtPercent.Value = ""
.txtMarkup.Value = ""
.txtTotal.Value = ""
End With
'give the user the thumbs up
MsgBox "The data has been sent to the database"
End Sub
'The user form is closed this code

Private Sub cmdClose_Click()


'close the userform
Unload Me
End Sub

'When the combo box value changes we need to clear all the other values

Private Sub cmdProducts_Change()


'clear the values if the products change
With Me
.txtQuantity.Value = ""

4
.txtKg.Value = ""
.txtPercent.Value = ""
.txtMarkup.Value = ""
.txtTotal.Value = ""
End With
End Sub

'This is the code that sends the values to the worksheet and then reassigns them back into the userform

Private Sub txtQuantity_AfterUpdate()


'check for values and datatype
If Not IsNumeric(Me.txtQuantity.Value) Or Me.cmdProducts = "" Then
MsgBox "The quantity needs to be a number and the Products needs to have a value"
Me.txtQuantity.Value = ""
Exit Sub
End If
'send the values to the worksheet
With Sheet1
.Range("M5").Value = Me.cmdProducts.Value
.Range("N5").Value = Me.txtQuantity.Value
End With
'retrieve the results
With Me
.txtKg.Value = Sheet1.Range("O5").Value
.txtKg.Value = Format(Me.txtKg.Value, "$#,##0.00")
.txtPercent.Value = Sheet1.Range("P5").Value
.txtPercent.Value = Format(Val(Me.txtPercent.Value) * 100, "0:00%")
.txtMarkup.Value = Sheet1.Range("Q5").Value
.txtMarkup.Value = Format(Me.txtMarkup.Value, "$#,##0.00")
.txtTotal.Value = Sheet1.Range("R5").Value
.txtTotal.Value = Format(Me.txtTotal.Value, "$#,##0.00")
End With
End Sub

Run the Userform


In the module to your VBA editor add the code below.
Right Click the shape on the worksheet and then choose Assign Macro and assign the macro below. This will call the
userform.

Sub Showme()
frmProducts.Show
End Sub

In conclusion
I hope you have enjoyed this tutorial to show how you can do calculations in a Userform quite simply. There are two
more tutorials to come were I will demonstrate creating a Bonus Calculator for employees and an Age and BMI
Calculator in Userforms.
Best wishes
Trevor

Vous aimerez peut-être aussi