How to do basic math calculations on VBA(Visual Basic Application)đ»
Visual Basic for Applications is a computer programming language developed and owned by Microsoft. With VBA you can create macros to automate repetitive word- and data-processing functions, and generate custom forms, graphs, and reports. Can do much more with it.

Subroutine procedures usually take arguments or code which are carried out to perform a specific task. Using the Sub procedure, we can set up the variable and type a variable to store a value inside the variable. After the naming, the sub procedure press enter key and âend subâ will add up automatically. So it starts with âsubâ and ends with âend subâ.
Dim statement is used for declaring variables and allocating storage space. To declare a variable we use dim statement.
Range property in VBA helps to specify a particular cell or group of cells or a row or a column.
Note: canât set up a variable and assign a value all on the same line. It shows you error.
đBasic Math Operators
đ§ Addition operator : Adding two values with a sub procedure is created. A âDimâ statement is used for declaring the variables, so over here the variables are âNumber_1â & âNumber_2â and also declare their data type as âIntegerâ for both the variables. Assigning values for both the variables. Next, we are mentioning where the calculation result is to be seen, like on which sheet, which cell or group of cells you want to select to see the value based on argument. So we are mentioning the worksheet = 1, select cell = A12,B12, Value = Addition Answer, Number_1 + Number_2(value). Now we can see the value âAddition Answerâ in âA12 cellâ and âNumber_1 + Number2â value in âB12 cellâ.
Worksheets(1): Inside bracket you can also mention the worksheet name as âSheet1â in double quotes. The number 1 means âworksheet 1" in the current workbook.
( â ) Single quote is used for commenting in VBA.
'Adding two numbers
Sub add_numbers()
Dim Number_1 As Integer
Dim Number_2 As Integer
Number_1 = 10
Number_2 = 30
Worksheets(1).Range("A12").Value = "Addition Answer"
Worksheets(1).Range("B12").Value = Number_1 + Number_2
End Sub
đ§ Subtraction operator: Subtracting two values with a sub procedure is created. A âDimâ statement is used for declaring the variables, so over here the variables are âNumber_1â & âNumber_2â and also declare their data type as âIntegerâ for both the variables. Assigning values for both the variables. Next, we are mentioning where the calculation result is to be seen, like on which sheet, which cell or group of cells you want to select to see the value based on argument. So we are mentioning the worksheet = 1, select cell = A13,B13, Value = Subtraction Answer, Number_1 -Number_2(value). Now we can see the value âSubtraction Answerâ in âA13 cellâ and âNumber_1-Number2â value in âB13 cellâ.
'Subtracting two numbers
Sub subtr_numbers()
Dim Number_1 As Integer
Dim Number_2 As Integer
Number_1 = 10
Number_2 = 30
Worksheets(1).Range("A13").Value = "Subtraction Answer"
Worksheets(1).Range("B13").Value = Number_1 - Number_2
End Sub
đ§ Multiplication Operator: Multiplying two values with a sub procedure is created. A âDimâ statement is used for declaring the variables, so over here the variables are âNumber_1â & âNumber_2â and also declare their data type as âIntegerâ for both the variables. Assigning values for both the variables. Next, we are mentioning where the calculation result is to be seen, like on which sheet, which cell or group of cells you want to select to see the value based on argument. So we are mentioning the worksheet = 1, select cell = A14,B14, Value = Multiplication Answer, Number_1 * Number_2(value). Now we can see the value âMultiplication Answerâ in âA14 cellâ and âNumber_1 * Number2â value in âB14 cellâ.
'Multiplying two numbers
Sub multiple_numbers()
Dim Number_1 As Integer
Dim Number_2 As Integer
Number_1 = 10
Number_2 = 30
Worksheets(1).Range("A14").Value = "Multiplication Answer"
Worksheets(1).Range("B14").Value = Number_1 * Number_2
End Sub
đ§ Division operator: Dividing two values with a sub procedure is created. A âDimâ statement is used for declaring the variables, so over here the variables are âNumber_1â & âNumber_2â and also declare their data type as âIntegerâ for both the variables. Assigning values for both the variables. Next, we are mentioning where the calculation result is to be seen, like on which sheet, which cell or group of cells you want to select to see the value based on argument. So we are mentioning the worksheet = 1, select cell = A15,B15, Value = Division Answer, Number_1 / Number_2(value). Now we can see the value âDivision Answerâ in âA15 cellâ and âNumber_1 / Number2â value in âB15 cellâ.
'dividing two numbers
Sub div_numbers()
Dim Number_1 As Integer
Dim Number_2 As Integer
Number_1 = 10
Number_2 = 30
Worksheets(1).Range("A15").Value = "Division Answer"
Worksheets(1).Range("B15").Value = Number_1 / Number_2
End Sub
đ§ Mixing addition and subtraction: Assigning more variables and doing calculations on addition and subtraction. Over here assigning variable âAnswerâ telling VBA to do this calculation(Number_1 + Number_2-Number_3). So we are mentioning the worksheet = 1, select cell = A16,B16, Value = Mix_add_sub, Answer. Now we can see the value âMix_add_subâ in âA16 cellâ and âAnswerâ value in âB16 cellâ.
'adding & subtracting numbers
Sub basic_maths()
Dim Number_1 As Integer
Dim Number_2 As Integer
Dim Number_3 As Integer
Dim Answer As Integer
Number_1 = 1
Number_2 = 5
Number_3 = 56
Answer = Number_1 + Number_2 - Number_3
Worksheets(1).Range("A16").Value = "Mix_add_sub"
Worksheets(1).Range("B16").Value = Answer
End Sub
đ§ Mixing addition and Multiplication: Assigning more variables and doing calculations on multiplication and addition. Over here assigning variable âAnswerâ telling VBA to do this calculation(Number_1 * Number_2 + Number_3). So we are mentioning the worksheet = 1, select cell = A17,B17, Value = Mix_add_multi, Answer. Now we can see the value âMix_add_multiâ in âA17 cellâ and âAnswerâ value in âB17 cellâ.
'Multiplying & adding numbers
Sub mix_add_multi()
Dim Number_1 As Integer
Dim Number_2 As Integer
Dim Number_3 As Integer
Dim Answer As Integer
Number_1 = 10
Number_2 = 45
Number_3 = 33
Answer = Number_1 * Number_2 + Number_3
Worksheets(1).Range("A17").Value = "Mix_add_multi"
Worksheets(1).Range("B17").Value = Answer
End Sub
Mixing division and Multiplication: Instead of mentioning the variable names, we can also mention values such as in Answer = 10 * 45 / 33. So we are mentioning the worksheet = 1, select cell = A18,B18, Value = Mix_multi_div, Answer. Now we can see the value âMix_multi_divâ in âA18 cellâ and âAnswerâ value in âB18 cellâ.
'Multiplying & dividing numbers
Sub mix_multi_div()
Dim Number_1 As Integer
Dim Number_2 As Integer
Dim Number_3 As Integer
Dim Answer As Integer
Number_1 = 10
Number_2 = 45
Number_3 = 33
Answer = 10 * 45 / 33
Worksheets(1).Range("A18").Value = "Mix_multi_div"
Worksheets(1).Range("B18").Value = Answer
End Sub
đ§ Mixing all math operators : Now we are using all math operators at a time. So we are mentioning the worksheet = 1, select cell = A19,B19, Value = Mix_add_sub_multi_div, Answer. Now we can see the value âMix_add_sub_multi_divâ in âA19 cellâ and âAnswerâ value in âB19 cellâ. If you are using parenthesis in your calculation, VBA always follows BODMAS rule.
'Using all math operators to do calculation
Sub Mix_add_sub_multi_div()
Dim Number_1 As Integer
Dim Number_2 As Integer
Dim Number_3 As Integer
Dim Number_5 As Integer
Dim Answer As Integer
Number_1 = 55
Number_2 = 23
Number_3 = 3
Number_4 = 12
Answer = 23 - 3 / (55 + 12) * 3
Worksheets(1).Range("A19").Value = "Mix_add_sub_multi_div"
Worksheets(1).Range("B19").Value = Answer
End Sub

Happy learning!!!đ
đ° If you like the article plz..do clap(click on the clap option below)đ it would be a support đand also do share it đ.
Follow me on đ:
â Medium
â YouTube