繁体   English   中英

Excel:如果B或C列中的单元格为空,则清除A列的单元格中的内容

[英]Excel: Clear Contents In Cell Of Column A If Cell In Column B Or C Is Empty

我想在Excel Office 2010中创建宏或公式。

如果B列或C列中的单元格为空,则清除A列中的内容。如果B列或C列中的单元格不为空,则不要清除A中的内容。

我有一个表格,范围从A1到C10,我想检查单元格B1:B10和C1:C10的值。

我发现该公式: =IF(ISBLANK(B1);"";"YOUR VALUE HERE")

但是,当我尝试使用它时,我遇到了一个问题:当为false时,A1中的内容变为0。

第一次使用此代码来更正所有数据(它会清除列A中您将无法恢复的数据),
您只需要将SheetName更改为工作表的名称:

Sub testMR()
    Dim i As Long
    Dim LastRow As Long
    Dim wS As Worksheet

    Set wS = ThisWorkbook.Sheets("SheetName")
    LastRow = LastRow_1(wS)

    For i = 1 To LastRow
        With wS
            If .Cells(i, 2) <> vbNullString Or .Cells(i, 3) <> vbNullString Then
            Else
                'B and C empty
                .Cells(i, 1) = vbNullString
            End If
        End With 'wS
    Next i
End Sub


Public Function LastRow_1(wS As Worksheet) As Double
    With wS
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            LastRow_1 = .Cells.Find(What:="*", _
                                After:=.Range("A1"), _
                                Lookat:=xlPart, _
                                LookIn:=xlFormulas, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, _
                                MatchCase:=False).Row
        Else
            LastRow_1 = 1
        End If
    End With
End Function

将其放置在您希望自动化行为的工作表模块中:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Rows.Count > 1 Then Exit Sub
    If Application.Intersect(Target, Me.Range("B:C")) Is Nothing Then Exit Sub

    With Me
        If .Cells(Target.Row, 2) <> vbNullString _
           Or .Cells(Target.Row, 3) <> vbNullString Then
        Else
            'B and C empty
            .Cells(Target.Row, 1) = vbNullString
        End If
    End With 'Me
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM