简体   繁体   English

Excel VBA代码根据另一个单元格的数据检查单元格是否为空白

[英]Excel VBA code to check if cell blank based on another cell's data

I have a spreadsheet that multiple users fill out and turn in daily.我有一个电子表格,多个用户每天填写并上交。 In this spreadsheet there are three separate Yes/No questions.在这个电子表格中有三个单独的是/否问题。 If they enter Yes, then in the following column they have to enter data.如果他们输入 Yes,那么他们必须在以下列中输入数据。 I want to make a VBA code to check and make sure this data is entered so we don't have to keep sending the spreadsheets back to the users to fill in missing data.我想制作一个 VBA 代码来检查并确保输入了这些数据,这样我们就不必继续将电子表格发回给用户来填写缺失的数据。

My data is set up like this: K12:K111, N12:N111, and P12:P111 are all the Yes/No columns while L12:L111, O12:O111, and Q12:Q111 are the cells that require text ONLY if a "Yes" was put in the K, N or P columns.我的数据是这样设置的:K12:K111、N12:N111 和 P12:P111 都是是/否列,而 L12:L111、O12:O111 和 Q12:Q111 是需要文本的单元格,如果“是”放在 K、N 或 P 列中。 Could someone help me with the coding for this?有人可以帮我编码吗?

I would ideally like to put an ActiveX button on the spreadsheet to run the VBA code if possible.如果可能,我希望在电子表格上放置一个 ActiveX 按钮来运行 VBA 代码。 I would also like for it to show a dialog box telling which cells need data entered.我还希望它显示一个对话框,告诉哪些单元格需要输入数据。 Any help would be greatly appreciated!任何帮助将不胜感激!

EDIT: I did change the Ranges from M to N as I had misspoken in my original post.编辑:我确实将范围从 M 更改为 N,因为我在原始帖子中说错了。 I used the code suggested below and am getting a Compile Error: Invalid inside procedure.我使用了下面建议的代码,但出现编译错误:内部过程无效。 This is how I pasted it in to correspond to the button:这是我粘贴它以对应按钮的方式:

Private Sub CommandButton2_Click()
Option Explicit

Sub test()

    Dim rngK As Range, rngN As Range, rngP As Range, cell As Range
    Dim Counter As Long

    Counter = 0

    With ThisWorkbook.Worksheets("Sheet1")

        Set rngK = .Range("K12:K111")
        Set rngN = .Range("N12:N111")
        Set rngP = .Range("P12:P111")

        For Each cell In rngK

            If cell.Value = "Yes" And cell.Offset(0, 1).Value = "" Then

                cell.Offset(0, 1).Interior.Color = vbRed
                Counter = Counter + 1

            End If

        Next cell

        For Each cell In rngN

            If cell.Value = "Yes" And cell.Offset(0, 1).Value = "" Then

                cell.Offset(0, 1).Interior.Color = vbRed
                Counter = Counter + 1

            End If

        Next cell

        For Each cell In rngP

            If cell.Value = "Yes" And cell.Offset(0, 1).Value = "" Then

                cell.Offset(0, 1).Interior.Color = vbRed
                Counter = Counter + 1

            End If

        Next cell

        If Counter > 0 Then

            MsgBox "Please fill red highlighted fields!"

        End If

    End With

End Sub

End Sub

You could try the below:你可以试试下面的:

Option Explicit

Sub test()

    Dim rngK As Range, rngM As Range, rngP As Range, cell As Range
    Dim Counter As Long

    Counter = 0

    With ThisWorkbook.Worksheets("Sheet1")

        Set rngK = .Range("K12:K111")
        Set rngM = .Range("M12:M111")
        Set rngP = .Range("P12:P111")

        For Each cell In rngK

            If cell.Value = "Yes" And cell.Offset(0, 1).Value = "" Then

                cell.Offset(0, 1).Interior.Color = vbRed
                Counter = Counter + 1

            End If

        Next cell

        For Each cell In rngM

            If cell.Value = "Yes" And cell.Offset(0, 2).Value = "" Then

                cell.Offset(0, 2).Interior.Color = vbRed
                Counter = Counter + 1

            End If

        Next cell

        For Each cell In rngP

            If cell.Value = "Yes" And cell.Offset(0, 1).Value = "" Then

                cell.Offset(0, 1).Interior.Color = vbRed
                Counter = Counter + 1

            End If

        Next cell

        If Counter > 0 Then

            MsgBox "Please fill red highlighted fields!"

        End If

    End With

End Sub

As per OP request:根据 OP 要求:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Me.Range("K12:K111, M12:M111,P12:P111")) Is Nothing Then

        With Target

            If UCase(.Value) = "YES" Then
                .Offset(0, 1).Interior.Color = vbRed
            Else
                .Offset(0, 1).Interior.Pattern = xlNone
            End If

        End With

    End If

    If Not Intersect(Target, Me.Range("L12:L111, O12:O111,Q12:Q111")) Is Nothing Then

        With Target

            If .Value = "" And UCase(.Offset(0, -1).Value) = "YES" Then
                .Offset(0, 1).Interior.Color = vbRed
            Else
                .Interior.Pattern = xlNone
            End If

        End With

    End If

End Sub

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

相关问题 EXCEL公式或VBA基于另一个单元格的数据提取数据 - EXCEL Formula or VBA To Extract Data Based on Another Cell's Data Excel VBA代码将基于单元格引用的数据复制到另一个工作表 - Excel VBA Code to Copy Data based on a cell reference to another sheet excel vba - 从基于另一列空白的变体中删除单元格 - excel vba - remove cell from a variant based on blank in another column Excel VBA代码可根据另一个单元格值清除一个单元格中的数据 - Excel VBA code to clear data from one cell based on another cell value VBA-Excel根据单元格值将数据导出到另一个工作簿 - VBA-Excel Export data to another workbook based on cell value 根据Excel中的另一个单元格自动填充单元格数据 - Autofill a cell data based on another cell in Excel 如果单元格为空白,则清除 VBA 中另一个单元格代码的内容 - if Cell is Blank then clear content of another cell Code in VBA 在VBA Excel 2010中根据另一个单元格的值设置一个单元格的值 - Set value of a cell based on another cell's value in VBA Excel 2010 Excel VBA检查其他单元格中是否包含单元格单词 - Excel VBA check if cell words contained in another cell Excel VBA - 如何在一个单元格中识别年份(日期),然后检查另一个单元格中的数据,然后计算 - Excel VBA - how to identify the year (date) in one cell then check another cell for the data there and then count
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM