Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

excel - Display the lowest value

Code Description : Using For Next write VBA macro to accept ten numbers and display (through message box) total, average, highest and lowest

Problem : Only if 1 is in the selection it shows the lowest value otherwise blank result.

Sub Codes()

    For counter = 1 To 10
        ActiveCell = InputBox("Please enter 10 numbers", " ")
        vartotal = vartotal + ActiveCell
        varaverage = vartotal / counter
        
        If ActiveCell > varhighest Then varhighest = ActiveCell
        If ActiveCell = 1 Then varlowest = ActiveCell Else If ActiveCell < varlowest Then varlowest = ActiveCell
    
        ActiveCell.Offset(1, 0).Select
    Next counter
        MsgBox "SumTotal = " & vartotal & vbNewLine & "Average = " & varaverage & vbNewLine & "Highest = " & varhighest & vbNewLine & "Lowest = " & varlowest
End Sub
question from:https://stackoverflow.com/questions/65928944/display-the-lowest-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Another method is to load an array then just use the worksheet formulas:

Sub Codes()
    Dim counter As Long
    Dim activecell(9) As Double
    Dim vartotal As Double
    Dim varhighest As Double
    Dim varlowest As Double
    Dim varaverage As Double

    For counter = 0 To 9
        activecell(counter) = Application.InputBox("Please enter 10 numbers", " ", , , , , , 1)
    Next counter
    vartotal = Application.Sum(activecell)
    varhighest = Application.Max(activecell)
    varlowest = Application.Min(activecell)
    varaverage = Application.Average(activecell)
    MsgBox "SumTotal = " & vartotal & vbNewLine & "Average = " & varaverage & vbNewLine & "Highest = " & varhighest & vbNewLine & "Lowest = " & varlowest
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...