[英]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.