如果不匹配时间,VBA会向下移动一些范围

时间:2015-06-08 14:18:45

标签: excel vba range

我有16k行数据。有两列时间。我需要的是找到时间不匹配的行,并在最后3列中向下移动每一行,所以最后我将拥有所有时间匹配的行和那些不会有最后3列空白的行行。 我到目前为止所拥有的,但我是VBA的新手,这不起作用(

Sub timeline()

Dim y As Long
    With ThisWorkbook.Sheets("L5")
    y = .Range("G" & .Rows.Count).End(xlUp).Row
    End With

x = 2
Do While ThisWorkbook.Sheets("L5").Cells(x, 4) <> ""


    If ThisWorkbook.Sheets("L5").Cells(x, 4).Value = ThisWorkbook.Sheets("L5").Cells(x, 7).Value Then

    Else: ThisWorkbook.Sheets("L5").Range("Gx:Iy").Select
    Selection.Offset(1, 0).Select

    y = y + 1

    End If

x = x + 1
Loop

1 个答案:

答案 0 :(得分:0)

以下代码应该这样做。检查是否使用了正确的范围和单元格;我试图从你的代码中弄明白:

Sub timeline()

    Dim y As Long
    With ThisWorkbook.Sheets("L5")
        y = .Range("G" & .Rows.Count).End(xlUp).Row
    End With

    x = 2
    Do While ThisWorkbook.Sheets("L5").Cells(x, 4) <> ""

        If ThisWorkbook.Sheets("L5").Cells(x, 4).Value = ThisWorkbook.Sheets("L5").Cells(x, 7).Value Then
            ' nothing
        Else
            ThisWorkbook.Sheets("L5").Range("G" & Trim(Str(x)) & ":I" & Trim(Str(y))).Cut
            ThisWorkbook.Sheets("L5").Range("G" & Trim(Str(x + 1))).Select
            ThisWorkbook.Sheets("L5").Paste
            y = y + 1
        End If

        x = x + 1
    Loop
End Sub