简体   繁体   English

Excel VBA有很多问题

[英]Excel VBA a lot of problems

So I'm new to VBA, and I am trying to get a macro to compare cells, and output a counter in the column next to it. 因此,我是VBA的新手,我正在尝试获取一个宏来比较单元格,并在其旁边的列中输出一个计数器。 Here is my code: 这是我的代码:

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 结束子

Okay so this code runs, and it looks it works, column "N" starts to populate, but the program freezes up, and I do not know if it is just because the file is so large that it takes a lot of time, or if something is wrong. 好的,这段代码可以运行,并且看起来可以正常工作,列“ N”开始填充,但是程序冻结了,我不知道是否仅仅是因为文件太大而花费大量时间,还是如果有什么问题。 If i restart the program i get run-time error '-2147417848 (80010108)': Method '_Default" of object "Range" failed. Any idea what that means? 如果我重新启动程序,则会出现运行时错误'-2147417848(80010108)':对象“范围”的方法“ _Default”失败了,知道这意味着什么吗?

Any help would be greatly appreciated, hopefully, I'm not just making dumb mistakes. 任何帮助将不胜感激,希望我不仅犯傻。

EDIT: Sub Duplicate_Count() 'Find the last used row in a Column: column A in this example 编辑:Sub 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

End Sub 结束子

This code crashes every time, and will occasionally give me an error of run-time error '-2147417848 (80010108)': Method '_Default" of object "Range" failed. I have no idea how to fix that... or what it even means. 这段代码每次都崩溃,偶尔会给我一个运行时错误“ -2147417848(80010108)”的错误:对象“ Range”的方法“ _Default”失败。我不知道该如何解决...它甚至意味着。

I don't get an error when I run your code, but I made some changes I think might fix it. 运行您的代码时没有出现错误,但是我进行了一些更改,我认为这可能会解决它。 Try this and let me know what happens! 试试这个,让我知道会发生什么!

 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

I just changed it after I saw your comment about it being a large column, I think this should be much faster! 在看到您对它的评论很大时,我才对其进行了更改,我认为它应该快得多!

Alrighty this is the code I finished with: 好的,这是我完成的代码:

Sub Duplicate_Count() 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 结束子

Thank you so much everyone for your help! 非常感谢大家的帮助! Turns out that the values are strings because I had some headers being looked at, and setting the worksheet was one of my biggest problems, as to it having the run time error, i believe that is just because the document is so long. 事实证明,这些值是字符串,因为正在查看一些标头,而设置工作表是我遇到的最大问题之一,因为它存在运行时错误,我认为那是因为文档太长了。 I let it sit for 30 min and it completed just fine. 我让它坐了30分钟,一切顺利。 Thanks again for the help everyone ! 再次感谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM