2017-06-21 61 views
0

所以我是vba的新手,我试图让宏来比较单元格,并在它旁边的列中输出一个计数器。这里是我的代码:Excel VBA很多问题

Sub Duplicate_Count() 

'Find the last used row in a Column: column A in this example 

Dim LastRow As Long 
Dim value1 As String 
Dim value2 As String 
Dim counter As Integer 
counter = 1 

With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "L").End(xlUp).Row 
End With 

'Search down row for duplicates 
Dim i As Long 

For i = 1 To LastRow 

    'Sets value1 and value2 to be compared 
    value1 = Worksheets("Sheet1").Cells(i, "L").Value 
    value2 = Worksheets("Sheet1").Cells(i + 1, "L").Value 

    'If values are not diferent then counter will not increment 
    If value1 <> value2 Then 
     counter = counter + 1 
    End If 

    'Sets the n colom to count, duplicates should not increment the counter 
    Sheet1.Cells(i, "N") = counter 

Next i 

末次

好了,所以这段代码运行,它看起来它的工作原理,列“N”开始填充,但程序冻结了,我不知道如果仅仅是因为文件太大以致需要很多时间,或者如果出现错误。如果我重新启动程序,我得到运行时错误'-2147417848(80010108)':对象“范围”的方法'_Default'失败。只是让愚蠢的错误

编辑: 子Duplicate_Count() “查找列中的最后一次使用的行:A列在这个例子中

Dim LastRow As Long 
Dim value1 As String 
Dim value2 As String 
Dim counter As Long 
counter = 0 
Dim sht As Worksheet 
Set sht = Worksheets("Sheet1") 

With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "L").End(xlUp).Row 
End With 

'Search down row for duplicates 
Dim i As Long 

For i = 1 To LastRow 

    'Sets value1 and value2 to be compared 
    value1 = Worksheets("Sheet1").Cells(i, "L").Value 
    value2 = Worksheets("Sheet1").Cells(i + 1, "L").Value 

    'If values are not diferent then counter will not increment 
    If value1 <> value2 Then 
     counter = counter + 1 
    End If 

    'Sets the n colom to count, duplicates should not increment the counter 
    sht.Cells(i, "N") = counter 

Next i 

末次

此代码崩溃每次,和偶尔会给我一个错误:运行时错误'-2147417848(80010108)':对象“Range”的方法'_Default'失败。我不知道如何解决这个问题......甚至是什么意思。

+0

我注意到的第一个潜在问题是您每次循环时都将计数器设置为1,您是否希望这样做?同时,每当你循环时,你都不需要'调暗'每个变量。 – Dexloft

+0

不,我意识到可以将其移出循环,谢谢! – Josh

+0

也许你可以使用简单= COUNTIF($ A $ 1:$ A $ 1000,A1)? – jkpieterse

回答

0

Alrighty这是我完成的代码:

子Duplicate_Count()

Dim LastRow As Long 
Dim value1 As String 
Dim value2 As String 
Dim counter As Long 
counter = 0 
Dim sht As Worksheet 
Set sht = Worksheets("Sheet1") 

'Find the last used row in Column L 
With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "L").End(xlUp).Row 
End With 

'Search down row for duplicates 
Dim i As Long 

For i = 1 To LastRow - 1 


    'Sets value1 and value2 to be compared 
    value1 = Worksheets("Sheet1").Cells(i, "L").Value 
    value2 = Worksheets("Sheet1").Cells(i + 1, "L").Value 

    'If values are not diferent then counter will not increment 
    If value1 <> value2 Then 
     counter = counter + 1 
    End If 

    'Sets the n colom to count, duplicates should not increment the counter 
    sht.Cells(i + 1, "N") = counter 

Next i 

末次

谢谢你这么多大家的帮助!原来,这些值是字符串,因为我看了一些标题,设置工作表是我最大的问题之一,至于它有运行时错误,我相信这只是因为文档太长了。我让它坐了30分钟,它完成得很好。 再次感谢大家的帮助!

0

我在运行代码时没有遇到错误,但我做了一些更改,我认为可能会修复它。试试这个,让我知道会发生什么!

Sub TommysMacro() 
    'Find the last used row in a Column: column A in this example 
    Dim LastRow As Long 
    Dim counter As Integer 

    LastRow = Sheets("Sheet1").Cells(65536, "L").End(xlUp).Row + 1 

    'Sets values to be compared 
    ReDim cellValue(1 To LastRow) As String 
    For i = 1 To LastRow 
     cellValue(i) = Worksheets("Sheet1").Cells(i, "L").Value 
    Next i 

    'Search down row for duplicates 
    For i = 1 To LastRow - 1 

     'If values are not diferent then counter will not increment 
     If cellValue(i) <> cellValue(i + 1) Then 
      counter = counter + 1 
     End If 

     'Sets the n column to count, duplicates should not increment the counter 
     Sheets("Sheet1").Cells(i, "N").Value = counter 

    Next i 

End Sub 

我刚刚改变它后,我看到你的评论关于它是一个大列,我认为这应该快得多!

+0

是'Sheet1'没有被定义,并给出了'Object Required'错误。我也会设置一个变量作为工作表。 'Dim sht as Worksheet'和'Set sht = Worksheets(“Sheet1”)'。现在,您可以使用'sht'而不是'Worksheets(“Sheet1”)',这使得代码更加方便。为了缩短这个时间,你可以在Dim'Mehtods之后为整个代码使用'With sht'。 – UGP

+0

什么是L65536? – Josh

+0

在Excel 2003和这样的旧版本中,它是可能的最大行值。既然你得到了一个错误,我没有(我正在使用Excel 2007),我想你可能有一个旧版本,所以我只是用它来防万一。基本上,它进入L列的最后一行,然后从那里使用'.End(xlUp)'获得L中最后一个使用的行。但是我注意到我的方法有错误,它不会执行最后一行在列中,因为我的循环去LastRow - 1 – Dexloft