Home > Blockchain >  VBA Code: How would you count even numbers and add them up based off of a single user input box
VBA Code: How would you count even numbers and add them up based off of a single user input box

Time:10-21

I am having trouble trying to figure out how to count even numbers when there is string that has a minimum and a maximum this string is a user input string. For example: enter image description here

  • But if the user just enters a single number, then that becomes the max. For example: enter image description here
Sub AddupEvenNumbers2()
    Dim num As Variant
    Dim evennum As Long
    Dim sum As Double
    Dim str As String
    Dim count As Integer
    Dim max As String
    Dim min As String

    str = "Please enter the beginning number " & vbNewLine & "and maximum number (4 22) " & vbNewLine & "or just the maximum number (22) " & vbNewLine & "to get the total sum. "
    
    Do
        num = InputBox(str)
        
        If num Like "* *" Then
            min = Split(num)(0)
            max = Right(num, min)
        Else
            max = IsNumeric(num)
        End If
        
    Loop While IsNumeric(num)
    evennum = num
    sum = 0
    For evennum = min To max Step 2
        sum = min   max
    Next
    MsgBox "The sum of even numbers " & vbNewLine & "from " & min & max & vbNewLine & "is " & sum

End Sub

CodePudding user response:

Your basic error is within you sum-routine: min max is not what you want. I fixed that error and improved the check of the input:

Sub AddupEvenNumbers2()
    Dim num As Variant
    Dim evennum As Long
    Dim sum As Double
    Dim str As String
    Dim count As Integer
    Dim max As Long
    Dim min As Long
    Dim fInputOK As Boolean

    Dim arrValues As Variant
    
    str = "Please enter the beginning number " & vbNewLine & "and maximum number (4 22) " & vbNewLine & "or just the maximum number (22) " & vbNewLine & "to get the total sum. "
    
    Do
        num = InputBox(str)
        If num = vbNullString Then Exit Sub 'cancel
        
        arrValues = Split(num)
        fInputOK = True
        
        'I improved the check for min and max value
        If num Like "* *" Then
            If IsNumeric(arrValues(0)) Then
                min = arrValues(0)
            Else
                MsgBox "wrong input"
                fInputOK = False
            End If
            If IsNumeric(arrValues(1)) Then
                max = arrValues(1)
            Else
                MsgBox "wrong input"
                fInputOK = False
            End If
        ElseIf IsNumeric(num) Then
            max = num
        Else
            MsgBox "wrong input"
            fInputOK = False
        End If
        
    Loop While fInputOK = False

    sum = 0
    For evennum = min To max Step 2
        sum = sum   evennum 'you have to add the evennum to the sum
    Next
    
    MsgBox "The sum of even numbers " & vbNewLine & "from " & min & " to " & max & vbNewLine & "is " & sum

End Sub
  • Related