Home > Back-end >  VBA Code: How would you count even numbers and add them together
VBA Code: How would you count even numbers and add them together

Time:10-20

  • I ask the user to put an input number like a "max" and then count and add all the even numbers together. For example: If the user inputs 6 then that would be 2 4 6 = 12
Sub AddupEvenNumbers()
    Dim num As Variant
    Dim evennum As Variant
    Dim sum As Double
    Dim str As String
    Dim count As Integer
    
    str = "Enter a upper/maximum number "
    num = InputBox(str)
    evennum = num
    
    If num Mod 2 Then
        evennum = num.Value   num
        count = count   1
    End If
    MsgBox "The sum of even numbers " & vbNewLine & "from 0 to " & num & vbNewLine & "is " & evennum

End Sub

CodePudding user response:

No need for VBA. You can use a formula: With the entered number in A1:

=SUM(SEQUENCE(A1)*ISEVEN(SEQUENCE(A1)))

In earlier versions of Excel, you can use an array-formula:

=SUM(ISEVEN(ROW(INDEX(A:A,1):INDEX(A:A,A1)))*ROW(INDEX(A:A,1):INDEX(A:A,A1)))

In some earlier versions of Excel, you may need to "confirm" this array-formula by holding down ctrl shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar

CodePudding user response:

In answer to your stated question of how to count even numbers less than an number (presumably over zero):

num / 2   1

To your question on how to sum these numbers: we know that opposite pairs of these values will sum to the original value (so for 6.... 4,2 = 6. So the number of pairs is half of count from the previous calculation, multiplied by the num value.

(num / 2   1) / 2 * num

Combined with your implied question of handling user errors, see code below. Note I don't use mod ever to test if even due to the fact that it forces the value to convert to long Long (see my Stack Overflow debut question on that topic...). So I use an int comparison shown below which also tests for decimals.

Sub ExampeEntry()
Dim aResponse As String, aNumberResponse As Long
       
start:
    aResponse = Replace(InputBox("Enter An Even Number"), ",", "") 'or "." if Europe
        
    If aResponse = "" Then
        'User cancelled/didn't enter
    ElseIf IsNumeric(aResponse) Then
        If Int(aResponse / 2) <> aResponse / 2 Then
            MsgBox aResponse & " is not an even number..."
            GoTo start
        Else
            'success
            aNumberResponse = aResponse
            MsgBox (aNumberResponse / 2   1)/2 * aNumberResponse
        End If
        
    Else
        MsgBox "ummm... """ & aResponse & """ isn't a number my friend...", vbCritical, "oh boy..."
        GoTo start
    End If
        
End Sub

CodePudding user response:

you need to loop using a For loop and step 2:

Sub AddupEvenNumbers()
    Dim num As Variant
    Dim evennum As Long
    Dim sum As Double
    Dim str As String
    Dim count As Integer
    
    str = "Enter a upper/maximum number "
    Do
        num = InputBox(str)
        If Not IsNumeric(num) Then str = "Must be a number." & vbNewLine & "Enter a upper/maximum number "
    Loop While Not IsNumeric(num)

    sum = 0

    For evennum = 0 To num Step 2
        sum = sum   evennum
    Next evennum
    
    MsgBox "The sum of even numbers " & vbNewLine & "from 0 to " & num & vbNewLine & "is " & sum

End Sub

Or use Application.InputBox(str,Type:= 1) to force a numeric entry

Sub AddupEvenNumbers()
    Dim num As Double
    Dim evennum As Long
    Dim sum As Double
    Dim str As String
    Dim count As Integer
    
    str = "Enter a upper/maximum number "

    num = Application.InputBox(str, Type:=1)
    
    sum = 0

    For evennum = 0 To num Step 2
        sum = sum   evennum
    Next evennum
    
    MsgBox "The sum of even numbers " & vbNewLine & "from 0 to " & num & vbNewLine & "is " & sum

End Sub
  • Related