计数将列中的单元格复制到最后一行

时间:2014-03-07 15:56:50

标签: excel vba excel-vba

我想计算列中最后一行的重复单元格,我可以将所有行计算到最后一行,但不能找出重复的部分。

如果找到解决方案,我会继续查看和回发解决方案。

编辑:更改为以下内容我收到错误“类型不匹配”

Sub ContactName22()

Dim lRow As Long
Dim count As Long

Sheets(2).Select
lRow = Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Row

count = "= SUMPRODUCT((E2 & lRow <>"""")/COUNTIF(E2 & lRow &"""")-(COUNTIF(E2 & lRow &"""")=1))"

MsgBox count

End Sub

2 个答案:

答案 0 :(得分:2)

以下是一个示例宏:

Sub CountDuplicates()
    Dim N As Long, cl As Collection
    Dim dCount As Long, V As Variant
    N = Cells(Rows.count, 1).End(xlUp).Row
    Set cl = New Collection
    dCount = 0
    For i = 1 To N
        V = Cells(i, 1).Value
        On Error Resume Next
        If V <> "" Then
            cl.Add V, CStr(V)
            If Err.Number = 0 Then
            Else
                Err.Number = 0
                dCount = dCount + 1
            End If
        End If
    Next i
    MsgBox "There are " & dCount & " duplicates"
End Sub

答案 1 :(得分:0)

Sub ContactName22()

'Declaring
Dim Sheets(2)As Worksheet
Set Sheets(2)= ThisWorkbook.Sheets("Sheets(2)")
Dim x As Long
Dim count As Long
Dim lastRow As Long
Dim lastColumn As Long

Sheets(2).Select

lastRow = dbSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = dbSheet.Cells(1, Columns.Count).End(xlToLeft).Column

   For y = 1 to LastColumn 'Looping through the columns until last column

     For x = 2 To lastRow 'Looping through the rows until last row 

            if ...... 'whatever condition that applies in your case  

            Then

             'do something else here

             count = count+1 'counting how many times has found duplicates based on the condition of `if` statment.

             End if

      Next x

 Next y

Cells(lastRow+1,2).Value=count 'assigning the value of the counter

End Sub