将该行的整个数据复制到另一个sheeet后,删除Excel vba中的空行

时间:2017-06-18 05:38:36

标签: excel vba

我正在尝试删除工作表中的emptry行。最初在该行中有一个数据,然后宏将切割该数据并移动到Sheet2,使其成为空行。 我希望删除该行,或者如果下面有任何行数据,我希望它们向上移动。

screenshot

Private Sub cmdMove_Click()

Dim myLog As Worksheet
    Dim myLogSheet As Range

    Dim i As Long
    i = 1

    i = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1

    Set myLog = Sheets("Sheet1")
    Set myLogSheet = myLog.Range("B:B").Find(txtID.Value, , , xlWhole)

    If Not myLogSheet Is Nothing Then
        myLogSheet.Offset(0, 2) = Format(Now, "hh:mm:ss")

        Application.Wait Now + TimeValue("00:00:02")    'Delay to see the punch out time...
        DoEvents

        myLogSheet.EntireRow.Cut Sheet2.Cells(i, "A")

        ' After cutting the entire row, I want the below data to move up to avoid any empty
        'row.. thank you.
        On Error Resume Next

    Else
        txtName.Value = "NO RECORD"
    End If

End Sub

2 个答案:

答案 0 :(得分:0)

将if部分替换为以下语句,它将起作用。

If Not myLogSheet Is Nothing Then
    Dim foundRow as long
    myLogSheet.Offset(0, 2) = Format(Now, "hh:mm:ss")
    foundRow = myLogSheet.row

    Application.Wait Now + TimeValue("00:00:02")    'Delay to see the punch out time...
    DoEvents

    myLogSheet.EntireRow.Cut Sheet2.Cells(i, "A")

    ' After cutting the entire row, I want the below data to move up to avoid any empty
    'row.. thank you.
    myLog.Rows(foundRow).select
    Selection.Delete Shift:=xlUp
    On Error Resume Next

Else
    txtName.Value = "NO RECORD"
End If

答案 1 :(得分:0)

在最后一行,添加此代码

Columns(1).SpecialCells(xlCellTypeBlanks).entirerow.Delete