【问题标题】:Excel VBA a lot of problemsExcel VBA 很多问题
【发布时间】:2018-07-24 02:39:11
【问题描述】:

所以我是 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)'的错误:对象“范围”的方法'_Default'失败。我不知道如何解决这个问题.. . 或者它甚至意味着什么。

【问题讨论】:

  • 我注意到的第一个潜在问题是您每次循环时都将计数器设置为 1,您希望它这样做吗?同时,你不需要每次循环时需要Dim每个变量
  • 不,我意识到可以将其移出循环,谢谢!
  • 您可以使用简单的 =COUNTIF($A$1:$A$1000,A1) 吗?
  • 好的,Dexloft 似乎已经修复了它。这是一个巨大的文件,所以需要很长时间才能完成,如果我有任何其他问题,我会告诉你。再次感谢!
  • jkpieterse,这可能会起作用,但是我要运行它的文件会改变长度,我宁愿不必编辑我需要运行宏的每个文件的函数。如果我明白的话您发布的内容...

标签: excel vba


【解决方案1】:

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

 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

看到你评论说它是一个很大的专栏,我才改了它,我认为这应该快得多!

【讨论】:

  • Sheet1 未定义并给出 Object Required 错误。我还将一个变量设置为工作表。与Dim sht as WorksheetSet sht = Worksheets("Sheet1")。现在您可以使用 sht 代替 Worksheets("Sheet1") ,这使代码更方便。为了更短,您可以在 Dim Mehtods 之后为整个代码使用 With sht
  • L65536 有什么用?
  • 在 Excel 2003 和类似的旧版本上,它是可能的最大行值。由于您遇到了错误而我没有(我使用的是 Excel 2007),我想您可能有一个旧版本,所以我只是使用它以防万一。基本上,它转到 L 列中的最后一行,然后从那里使用.End(xlUp) 来获取 L 中最后使用的行。但是我注意到我的方法中有一个错误,它没有执行列中的最后一行,因为我的循环转到 LastRow - 1
  • @UGP,是的,我总是看到人们这样做,但我从来没有真正喜欢过它的外观,有时我什至发现自己很难理解。你认为这些是我应该开始做的事情,还是你认为这更像是个人喜好?
  • 在这样的短代码上,实际上没有必要使用with,但我肯定会为工作表使用一个变量。如果您需要调整任何内容并且写作更容易和简短,它会有所帮助。我建议每次都使用它。
【解决方案2】:

好的,这是我完成的代码:

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

结束子

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多