如果K列中的单元格返回FALSE,请清除B列中的单元格

时间:2019-05-16 00:11:27

标签: excel vba

我有一个文件,用户可以在其中输入位置SKU的B列中的信息。

如果用户通过显示True或False在B列中输入了正确的信息,则将显示K列。

我要完成的工作是,如果K列返回False,B列中的信息将被清除,成为活动单元格,并且用户将需要输入正确的信息以返回True值。

我下面的代码仅限于单元格B2。

请问有人可以提供帮助吗?

非常感谢您!

我希望完成的图片:https://imgur.com/a/TGBMTkc

Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim cell22 As Range, b As Boolean Application.EnableEvents = False For Each cell22 In Target If Not Application.Intersect(cell22, Range("k2:k6000")) Is Nothing Then If cell22.Value <> True Then Range("B2").ClearContents Range("B2").Activate b = True End If End If Next cell22 If b Then MsgBox "xxx", vbInformation, "Important:" Application.EnableEvents = True End sub

1 个答案:

答案 0 :(得分:0)

尝试一下:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range, rng As Range, hadProblem As Boolean

    'only check cells in ColB
    Set rng = Application.Intersect(Me.Range("B:B"), Target)
    DoEvents 'allow for calculations

    On Error GoTo haveError

    If Not rng Is Nothing Then
        For Each c In rng.Cells
            'check "K" value
            If Not c.EntireRow.Cells(1, "K").Value Then
                Application.EnableEvents = False
                c.ClearContents
                Application.EnableEvents = True
                c.Select
                hadProblem = True
            End If
        Next c
    End If

    If hadProblem Then
        MsgBox "One or more cells in Column B had incorrect locations. Please rescan.", _
                vbExclamation, "Location Error"
    End If

    Exit Sub

haveError:
    Application.EnableEvents = True 'make sure events are turned back on

End Sub
相关问题