遍历行,选择相同的列,下一行直到上一行1000

时间:2019-04-15 23:26:36

标签: vba

我在一行中有一个单元格区域,并且我的代码执行了应有的工作,但是我需要遍历多达1000行,执行与粘贴的第一行相同的过程。如何遍历其他行,每行选择相同的列和单元格?

我使用了以下内容,并且有效,但是仅对于一行,我需要能够遍历那些直到1000行的列中的单元格,我该怎么做?

Dim cel As Range

    'Find the 2nd most recent date:
BeforeDate = Application.WorksheetFunction.Large(Range("Q2,T2,W2,Z2,AC2,AF2"), 2)

For Each cel In Range("Q2,T2,W2,Z2,AC2,AF2")
    If cel.Value < BeforeDate Then
        cel.ClearContents
        cel.Offset(, -1) = ""
        cel.Offset(, -2) = ""
    End If
Next cel

1 个答案:

答案 0 :(得分:0)

未经测试: (编辑-添加了一些错误检查)

Sub Tester()

    Dim cel As Range, rng As Range, x As Long, BeforeDate, v

    For x = 2 To 1000

        'this works because Range is *relative to the Row*
        Set rng = ActiveSheet.Rows(x).Range("Q1,T1,W1,Z1,AC1,AF1")

        BeforeDate = Application.Large(rng, 2)

        If Not IsError(BeforeDate) Then
            For Each cel In rng.Cells
                v = cel.Value
                If Not IsError(v) Then
                    If v < BeforeDate Then
                        cel.Offset(, -2).Resize(1, 3).ClearContents
                    End If
                End If
            Next cel
        End If

    Next x

End Sub