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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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,224,938
Messages
6,181,870
Members
453,068
Latest member
DCD1872

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