Excel精英培训网

 找回密码
 注册

QQ登录

只需一步,快速开始

你正在寻找更好的Excel学习教程吗?Excel技巧80集+数据透视表+函数初中高全套+VBA80集,想学的这儿全都有
查看: 151|回复: 1

[求助] vba代码编写求平均值

[复制链接]
发表于 2020-1-17 00:50 | 显示全部楼层 |阅读模式
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
怎么在这个代码里加入求平均值的代码,让整个代码变成点击按钮,输入几个数字后求出平均值
OptionExplicit
Subbtn_SumDigits()
    'Enter 2,3 or 4 numbers
    Dim SumTotal As Single, Numbers As Single,NumNumbers As Integer
    Dim One As Integer, Two As Integer, ThreeAs Integer, Four As Integer
    Dim ValidInput As Boolean
    ValidInput = False
    Do While ValidInput = False
        'Converts what they enter to a number(value)
        'String data becomes 0
        Numbers = Val(InputBox("Enter a 2to 4 digit number"))
        'Make sure enter a number between 2 and4 length
        'Note converts the number to a stringto find length (CStr)
        If Len(CStr(Numbers)) >= 2 AndLen(CStr(Numbers)) <= 4 Then
            'Only way for the loop to stop!!
            ValidInput = True 'User enteredlegitimate value
            NumNumbers = Len(CStr(Numbers))
            One = Mid(Numbers, 1, 1)
            Two = Mid(Numbers, 2, 1)
            SumTotal = One + Two
            If NumNumbers > 2 Then 'Have 3or 4 numbers
                Three = Mid(Numbers, 3, 1)
                SumTotal = SumTotal + Three
                If NumNumbers > 3 Then 'Have4 numbers
                  Four = Mid(Numbers, 4, 1)
                  SumTotal = SumTotal + Four
                End If
            End If
         End If
    Loop
    MsgBox "The total of the digits "& Numbers & " is " & SumTotal
EndSub

发表于 2020-1-17 10:35 | 显示全部楼层
Sub btn_SumDigits()

    'Enter 2,3 or 4 numbers
    Dim SumTotal As Single, Numbers As Single, NumNumbers As Integer
    Dim One As Integer, Two As Integer, Three As Integer, Four As Integer
    Dim ValidInput As Boolean
    Dim Avg As Single

    ValidInput = False
    Do While ValidInput = False

        'Converts what they enter to a number(value)
        'String data becomes 0
        Numbers = Val(InputBox("Enter a 2 to 4 digit number", , 123))

        'Make sure enter a number between 2 and4 length
        'Note converts the number to a stringto find length (CStr)
        If Len(CStr(Numbers)) >= 2 And Len(CStr(Numbers)) <= 4 Then

            'Only way for the loop to stop!!
            ValidInput = True 'User enteredlegitimate value
            NumNumbers = Len(CStr(Numbers))

            One = Mid(Numbers, 1, 1)
            Two = Mid(Numbers, 2, 1)
            SumTotal = One + Two
            Avg = SumTotal / 2

            If NumNumbers > 2 Then 'Have 3or 4 numbers
                Three = Mid(Numbers, 3, 1)
                SumTotal = SumTotal + Three
                        Avg = SumTotal / 3

                If NumNumbers > 3 Then 'Have4 numbers
                  Four = Mid(Numbers, 4, 1)
                  SumTotal = SumTotal + Four
                          Avg = SumTotal / 4

                End If
            End If
         End If
    Loop

    MsgBox "The total of the digits " & Numbers & " is " & SumTotal
    MsgBox "The total of the digits " & Numbers & " is " & Avg
End Sub

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2020-1-25 13:36 , Processed in 0.046800 second(s), 3 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表