VBA宏不会到文件excel的末尾

时间:2016-03-22 05:45:09

标签: excel vba excel-vba

我有一个放0而不是空行的宏有超过65000个一切正常但问题是宏停止在62000行,即使下一行有数据。这是代码:

Sub QuickCull()
    On Error Resume Next
    Columns("a").SpecialCells(xlBlanks).EntireRow.Delete
    Columns("b").SpecialCells(xlBlanks).EntireRow.Delete
    Columns("d").SpecialCells(xlBlanks).EntireRow.Delete

    Dim col As Range
    Set col = Cells(Rows.Count, "E").End(xlUp)

    Dim r As Range
    Set r = Range("E2", col).Resize(, 4)

    Dim cell As Range
    For Each cell In r
        If cell.Value = "" Then
           cell.Value = 0
        Else
           cell.Value = 1
        End If    
    Next cell

    Cells("J1").Activate

End Sub

在我看来,问题在于范围,但根本不确定。什么可能导致这种情况?

1 个答案:

答案 0 :(得分:1)

Range.Cells property不接受与Range object相同的单元格地址引用样式。

Cells("J1").Activate
'should be,
Range("J1").Activate

如果E:H列的值更改为1并且其空白更改为0,则可以使用Range.SpecialCells methodxlCellTypeBlanks继续xlCellTypeConstants

Sub QuickCull()
    Dim col As Range, r As Range

    With Worksheets("data")   '<~~ you should know ehat worksheet you are on!
        On Error Resume Next
        .Columns("a").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        .Columns("b").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        .Columns("d").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

        Set col = .Cells(Rows.Count, "E").End(xlUp)
        Set r = .Range("E2", col).Resize(col.Row - 1, 4)

        r.SpecialCells(xlCellTypeConstants) = 1
        r.SpecialCells(xlCellTypeBlanks) = 0

        .Range("J1").Activate   '<~~ or .Cells(4, "J").Activate
    End With
End Sub