excel - 当单元格值不等于“X”时,是否有打开 msgbox 的 vba 代码?
问题描述
我不精通vba,所以我认为这是最好的提问地方。我的目标是在特定单元格不等于“X”值时出现一个 msgbox。这是我下面的代码
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myCell As Range
For Each myCell In Range("G4:G160")
If myCell.Value <> 17521 Then
MsgBox "INCORRECT SKU RECHECK PALLET AND INFORM SUPERVISOR"
Exit Sub
End If
Next myCell
End Sub
单元格值取自另一个单元格,因此无需手动输入。当我运行此宏时,每次更改单元格或单击时它都会出现。数字正确时也会出现。我只需要这个 msgbox 仅在它不等于该值时出现。
我还希望消息是红色的。如果这很容易,请让我知道该怎么做或改变。对我来说最大的学习曲线,因为我总是避免使用 vba 代码。
编辑:请看下面的链接。右边的 1,2 和 3 的条形码是正确的,可以扫描到 B4 等等,错误的 4 和 5。我希望 msgbox 或 userform 能直观地显示错误的。如果输入正确,则不会弹出。
在此处输入图像描述 https://1drv.ms/x/s!AvRcvV5GEpBXnRIrzT2xDTv0iTQh?e=tV3LeN
解决方案
Your code seems to be right. Once you select another cell in your target sheet, this code runs and will never show the msgbox if all values in the range "G4:G160" are equal to 17521. If it appears, there is certainly at least one value <>17521.
I recommend to use the Change Event
instead:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCell As Range
For Each myCell In Range("G4:G160")
If (Not IsEmpty(myCell)) And myCell.Value <> 17521 And myCell.Value <> "" Then
MsgBox "INCORRECT SKU RECHECK PALLET AND INFORM SUPERVISOR", vbCritical
Exit Sub
End If
Next myCell
End Sub
About the red color of the msgbox, you cannot change the color of a msgbox. You have two options:
- Instead of red color, show a
Critical
icon as:
MsgBox "INCORRECT SKU RECHECK PALLET AND INFORM SUPERVISOR", vbCritical
In this case, you'll see something like this:
- Create a UserForm and adjust its font color and then show it instead of MsgBox. If you want to use the second option, please let me know and then help you create it.
推荐阅读
- python - python:如何插入闭合轮廓
- sql - 有条件地删除重复记录
- uinavigationbar - SwiftUI 隐藏导航栏影响其他动画
- python - 为什么 BeautifulSoup 无法在 Google 上获取所有 html?
- java - 如何使用一个 talend 作业根据 ftphost 名称连接到多个 ftp 站点
- php - PHP中的用户定义函数,变量有问题吗?
- python - 在 Google Colab Pro 中使用 TPU v3
- android - 避免使用 getIdentifier 检索文本资源的方法?
- javascript - 我在子组件中的 img 没有因父组件传递新状态而改变
- database - greenplum 分区如何支持 varchar 数据类型