如何删除第二个直接单元格后面没有的值?

时间:2017-10-05 08:57:48

标签: excel excel-formula

如何删除第二个直接单元格后面没有的值?

How to delete the values in Cell A which are not followed by the values in cell B

2 个答案:

答案 0 :(得分:0)

使用此宏。在常规模块中输入它(eq模块1)。如果列A中相邻单元格中没有值,它将删除列B中的所有值。

Sub delete()
For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    If Range("B" & x).Value <> "" Then
    Else
       Range("A" & x).ClearContents
    End If
Next x
End Sub

或者如果你想删除这些行。

Sub deleteRows()
For x = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
    If Range("B" & x).Value <> "" Then
    Else
       Range("A" & x).EntireRow.delete
    End If
Next x
End Sub

答案 1 :(得分:0)

以下可能有所帮助。

Sub Demo()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim cel As Range

    Set ws = ThisWorkbook.Sheets("Sheet5")  'change Sheet5 to your data sheet
    With ws
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For Each cel In .Range("B1:B" & lastRow)
            If IsEmpty(cel) Then    'or use If Len(cel) = 0 Then
                cel.Offset(0, -1).ClearContents
            End If
        Next cel
    End With
End Sub

如果要删除Column B为空的行,请尝试使用

Sub Demo()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet5")  'change Sheet5 to your data sheet
    ws.Range("B1:B100").SpecialCells(xlCellTypeBlanks).EntireRow.Delete 'change range as per your data
End Sub