伙计,我是 VBA 语言的初学者,我有一个问题要解决。
如何制作一个宏脚本来检查B列的任何行是否为“C”的输入词且C列的任何行为空,然后它会触发以颜色突出显示该行并提示消息框以提醒用户纠正它。
此外,D 列正在使用公式和逐个单元格方法来检查上述要求。
=IF(ISBLANK(B4),"",IF(OR(B4="C",B4="O"),IF(AND(B4="C", ISBLANK(C4)),"WARNING: Case Closed! Please Write Down Resolution!",""),"ERROR: Invalid Value - Status! Please Input The Right Value!"))
例如,第 4 行满足要求并受到影响。
有没有办法这样做?请帮忙。谢谢。
更新:感谢 Variatus!
当我保存文件时,它会提示这个消息框。我能做什么?谢谢。
微距屏幕
错误
Under normal circumstances you would be asked to show more of an own effort before receiving help on this forum, including from me. But apparently circumstances aren't normal. So, here we go. Paste this procedure to a standard code module (it's name would be a variation of Module1 by default).
Option Explicit
Sub MarkErrors()
' 283
Dim Spike() As String
Dim i As Long ' index of Spike
Dim Rl As Long ' last used row
Dim R As Long ' loop counter: rows
Application.ScreenUpdating = False
With Sheet1 ' this is the sheet's CodeName (change to suit)
.UsedRange.Interior.Pattern = xlNone ' remove all existing highlights
Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
ReDim Spike(1 To Rl)
For R = 2 To Rl
If Trim(.Cells(R, "B").Value) = "C" Then
If IsEmpty(.Cells(R, "C")) Then
.Range(.Cells(R, "A"), .Cells(R, "D")).Interior.Color = vbYellow
i = i + 1
Spike(i) = "Row " & R
End If
End If
Next R
End With
Application.ScreenUpdating = True
If i Then
ReDim Preserve Spike(1 To i)
MsgBox "Status errors were found in the following entries:-" & vbCr & _
Join(Spike, "," & vbCr), vbInformation, "Corrections required"
End If
End Sub
Pay attention to the specified worksheet Sheet1
. This is a CodeName, and it is a default. Excel will create a sheet by that name when you create a workbook. The CodeName doesn't change when the user changes the tab name but you can change it in the VB Editor. It's the (Name)
property of the worksheet.
在Sheet1
(不是标准代码模块,因此与您安装上述代码的位置不同。此模块由 Excel 为每个工作簿中的每个工作表创建。使用现有的一个。
Private Sub Worksheet_Activate()
' 283
MarkErrors
End Sub
这是一个事件过程。只要Sheet1
被激活(选择),它就会自动运行。因此,在正常情况下,您不需要手动运行第一个过程。但我已经谈过情况了。他们并不总是正常的。:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句