比较两列VBA宏时Excel崩溃

时间:2016-03-31 13:50:56

标签: excel vba excel-vba

我有两列我正在比较相同的条目,并通过Offset将匹配推送到另一列。当我运行我已构建的宏(从某些Microsoft固定代码中删除)时,它基本上会冻结并崩溃,因为它是基于所使用的单元格的每个循环的嵌套,我想它会在到达空单元格时结束,但我担心我可能处于无限循环中。任何帮助都感激不尽。

Dim myRng As Range
Dim lastCell As Long

Dim lastRow As Long
lastRow = ActiveSheet.UsedRange.Rows.Count

Dim c As Range
Dim d As Range

For Each c In Worksheets("Sheet1").Range("AT2:AT" & lastRow).Cells
    For Each d In Worksheets("Sheet1").Range("AU2:AU" & lastRow).Cells
        If c = d Then c.Offset(0, 1) = c
    Next d
Next c

5 个答案:

答案 0 :(得分:1)

试试这个:

cmake_minimum_required(VERSION 2.8)

project(DependsAllTest2)

macro(add_library _target)
    _add_library(${_target} ${ARGN})
    add_dependencies(MainProject ${_target})
endmacro()

macro(add_executable _target)
    _add_executable(${_target} ${ARGN})
    add_dependencies(MainProject ${_target})
endmacro()

macro(add_custom_target _target)
    _add_custom_target(${_target} ${ARGN})
    add_dependencies(MainProject ${_target})
endmacro()

add_subdirectory(MainProject)
add_subdirectory(ProjectA)
add_subdirectory(ProjectB)
add_subdirectory(ProjectC)
add_subdirectory(ProjectD)

而不是选择范围然后循环通过它们,这不需要做同样的事情。选择任何东西。如果找到匹配,它也会提前突破内循环。

答案 1 :(得分:1)

我认为这里存在多个问题:

  1. 搜索方法的效率
  2. Excel的响应能力丧失
  3. 如果可以将所有值都拉入数组,则可以显着提高代码的效率。这可以防止VBA在访问Excel对象模型和返回时花费的时间。可以使用DoEvents来处理响应能力的丧失。请尝试下面的代码。它可能看起来很长,但应该很容易理解。

        'Find last row
        Dim lastRow As Variant
        lastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
    
        'Create dynamic arrays
        Dim AT() As Variant: Dim AU() As Variant: Dim AV() As Variant
        ReDim AT(2 To lastRow): ReDim AU(2 To lastRow): ReDim AV(2 To lastRow)
    
        'Get all contents from Excel
        For i = 2 To lastRow
            AT(i) = Worksheets("Sheet1").Cells(i, 46)
            AU(i) = Worksheets("Sheet1").Cells(i, 47)
        Next i
    
        'Do the comparison
        For c = 2 To lastRow
    
            For d = 2 To lastRow
                If AT(c) = AU(d) Then AV(c) = AT(c)
            Next d
    
            'Allow a brief breather to Excel once in a while (don't hang)
            If (c / 100) = Int(c / 100) Then DoEvents
    
        Next c
    
        'Place final contents to Excel
        For i = 2 To lastRow
            Worksheets("Sheet1").Cells(i, 48) = AV(i)
        Next i
    

答案 2 :(得分:1)

试试这个循环:

Dim StartRange As Range, j As Long
Dim CompareRange As Range, i As Range

With Worksheets("Sheet1")
    Set StartRange = .Range("AT1", .Range("AT:AT").Find("*", , , , xlByRows, xlPrevious))
    Set CompareRange = .Range("AU1", .Range("AU:AU").Find("*", , , , xlByRows, xlPrevious))

    For Each i In StartRange
        i.Offset(, -8).Value = .Evaluate("IF(COUNTIF(" & CompareRange.Address(0, 0) & "," & i.Address(0, 0) & ")>0," & i.Value & ","""")")
    Next i
End With

答案 3 :(得分:0)

.icons a{
    display: inline-block;
    height: 64px;
    overflow: hidden;
    text-indent: -9999px;   /* Hide text in spans this way so it's accessible */
    width: 64px;
}

.icons .twitter span {
    visibility: hidden; /* let's not hide that text for accessibility purposes */
}
.icons .twitter a {
    text-decoration:none;
    background:url('images/i_twitter.png') no-repeat;
}

答案 4 :(得分:0)

我终于得到了它的工作,在接受了建议并将它们实现到我的代码中之后,我能够看到错误的实际位置,我在代码的前面引用了错误的列,并且通过这个,创建了没有重复的条目为了匹配,所以在修复之后,现在出现了匹配,我最终抵消它们,并将值更改为"是"反映我的图表中的重复。

谢谢大家的帮助。