如果A列中的单元格为空或具有值“Null”,则删除整行。删除这些行后,我需要粘贴在anather工作表中

时间:2015-01-11 14:34:02

标签: excel excel-vba vba

有人可以帮忙吗?

宏:

如果A列中的单元格为空或具有值" Null"则删除整行。删除这些行后,我需要粘贴在anather工作表中。

如果工作表2已经有一些行,那么这些新删除的工作表1中的行需要放在工作表2中的那些工作行的下面。

请提供一些宏代码。

感谢

2 个答案:

答案 0 :(得分:0)

好的,我已经创建了以下示例Sheet1,我将用它来说明问题的答案。

Names:         Sales
Kevin          5
Bill           2
Joe            4
Steve          5

Frank          2
Jerry          9


Casey          5

您会注意到第6,9和10行是空白的。

表二看起来像这样:

Names:       Sales
James        7
Phill        3

我们需要我们的代码首先删除Sheet1上的空白行,然后将这些行复制到Sheet2并将它们放在工作表的末尾。

Sub deleteBlanksAndCopy()
Dim i As Integer, j As Integer
Dim numRowsSheet1 As Integer

'This code will delete the blanks on sheet1
With ThisWorkbook.Worksheets("Sheet1")
    numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row

    For i = numRowsSheet1 To 1 Step -1
        If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Then
            .Cells(i, 1).EntireRow.Delete
        End If
    Next i
End With

'This code will move the rows from sheet1 to sheet2
With ThisWorkbook.Worksheets("Sheet1")
    numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With ThisWorkbook.Worksheets("Sheet2")
    j = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For i = 1 To numRowsSheet1
    ThisWorkbook.Worksheets("Sheet2").Rows(j + 1).Value = ThisWorkbook.Worksheets("Sheet1").Rows(i).Value
    j = j + 1
Next i
End Sub

谢谢,希望这有帮助。

答案 1 :(得分:0)

Private Sub CommandButton1_Click()  
Dim i As Integer, j As Integer  
Dim numRowsSheet1 As Integer

'此代码将找到Sheet2中的最后一行

With ThisWorkbook.Worksheets("Sheet2")
    j = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With

'此代码将从sheet1复制A列中具有空白单元格的行,并将它们粘贴到Sheet2上

With ThisWorkbook.Worksheets("Sheet1")
    numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row

    For i = 1 To numRowsSheet1 + 1 Step 1
        If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Or .Cells(i, 1) = "Null" Then

ThisWorkbook.Worksheets("Sheet2").Rows(j + 1).Value = ThisWorkbook.Worksheets("Sheet1").Rows(i).Value
           j = j + 1

        End If
    Next i
End With

'此代码将删除sheet1上的空白

With ThisWorkbook.Worksheets("Sheet1")
   numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row

   For i = numRowsSheet1 + 1 To 1 Step -1
        If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Or .Cells(i, 1) = "Null" Then
            .Cells(i, 1).EntireRow.Delete
        End If
    Next i
End With

End Sub