2010年12月31日星期五

两个Excel宏,方便统计数据

第一个,将选中的单元格中的数据合并到第一个单元格中,并清除其他单元格

第二个,将选中的单元格中的数据相加,结果到第一个单元格中,并清除其他单元格

Sub LinkCells()
    Dim cell As Object
    Dim count As Long
    Dim str As String
    str = ""
    count = 0
    For Each cell In Selection
        If str = "" Then
            str = str & cell.Value
        Else
            str = str & "," & cell.Value
        End If
        cell.Value = ""
    Next cell
    
    Cells(Selection.Row, Selection.Column).Value = str
End Sub


Sub AddCells()
    Dim cell As Object
    Dim count As Long
    Dim str As Integer
    str = 0
    count = 0
    For Each cell In Selection
       str = str + CInt(cell.Value)
       cell.Value = ""
    Next cell
    
    Cells(Selection.Row, Selection.Column).Value = str
End Sub

没有评论:

发表评论