Perform Calculation in VBA

lilybezer

New Member
Joined
Sep 29, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hi, I wrote a code in VBA for calculating the simple formula as gaussSum= [n*(n+1)] / 2, the number is given by the user. But the program does not perform the calculation it just gives me the number inputted by the user. How can I connect these two, so the program could take that number and then perform the calculation according to the formula?
Thanks in advance!

Option Explicit

Sub sumCalculation()
Dim userContribution As Integer
Dim gaussSum As Integer

userContribution = InputBox("Please state a number", "Need a number for Gauss Sum calculation")

gaussSum = calculate(userContribution)
printValue userContribution

End Sub

Function calculate(userContribution As Integer) As Integer
Dim result As Integer


result = userContribution * (userContribution + 1) / 2

calculate = result


End Function
Sub printValue(result As Integer)

Debug.Print "The Gauss Sum is " & result

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Welcome to MrExcel!

Perhaps this line
Rich (BB code):
printValue userContribution
should look like this
VBA Code:
printValue gaussSum
 
Upvote 0
Solution

Forum statistics

Threads
1,214,983
Messages
6,122,588
Members
449,089
Latest member
Motoracer88

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