所以我是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
End Sub
好的,所以这段代码运行了,它看起来有效,列&#34; N&#34;开始填充,但程序冻结了,我不知道是不是因为文件太大而需要花费很多时间,或者出现问题。如果我重新启动程序,我会得到运行时错误&#39; -2147417848(80010108)&#39;:方法&#39; _Default&#34;对象&#34;范围&#34;失败。知道这意味着什么吗?
我们非常感谢任何帮助,希望我不仅仅是犯了愚蠢的错误。
编辑: Sub Duplicate_Count() &#39;查找列中最后使用的行:此示例中的列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
End Sub
此代码每次都会崩溃,偶尔会给我一个运行时错误错误&#39; -2147417848(80010108)&#39;:Method&#39; _Default&#34;对象&#34;范围&#34;失败。我不知道如何解决这个问题......或者甚至意味着什么。
答案 0 :(得分: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
我刚刚看到你关于它是一个大栏目的评论后我才改变它,我认为这应该快得多!
答案 1 :(得分:0)
好的,这是我完成的代码:
Sub 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
End Sub
非常感谢大家的帮助!事实证明这些值是字符串,因为我看了一些标题,设置工作表是我最大的问题之一,因为它有运行时错误,我相信这只是因为文档太长了。我让它坐了30分钟就完成了。 再次感谢大家的帮助!