美文网首页
累加和连接

累加和连接

作者: 护国寺小学生 | 来源:发表于2019-01-31 19:02 被阅读0次

    Private Sub CommandButton1_Click()

        Dim i As Integer

        '获取非空格的行数

        endrow = Sheets("sheet1").Range("a" & Rows.Count).End(xlUp).Row

        '创建数组

        arr = Sheets("sheet1").Range("a2:a" & endrow, "b2:b" & endrow)

        Dim sum1 As Variant

        Dim sum2 As Variant

        sum1 = 0

        sum2 = 0

        '数组中取值叠加

        For i = 1 To UBound(arr)

            sum1 = sum1 + arr(i, 1)

        Next i

        Range("a1").Value = sum1

        For i = 1 To UBound(arr)

            sum2 = sum2 + arr(i, 2)

        Next i

        Range("b1").Value = sum2

    End Sub


    Private Sub CommandButton2_Click()

        Dim i As Integer

        endrow = Sheets("sheet1").Range("a" & Rows.Count).End(xlUp).Row

        arr = Sheets("sheet1").Range("a2:a" & endrow)

        Dim sum As String

        sum = ""

        For i = 1 To UBound(arr)

            sum = sum & arr(i, 1)

        Next i

        Range("d1").Value = sum

        MsgBox (sum)

    End Sub

    相关文章

      网友评论

          本文标题:累加和连接

          本文链接:https://www.haomeiwen.com/subject/wduwtqtx.html