检查范围内的所有值是否相同 [英] Check that all values in a range are identical

查看:57
本文介绍了检查范围内的所有值是否相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当电子表格中某个范围内的所有值均为零时,我需要显示一个消息框.目前,我正在使用以下代码:

I need to display a message box when all the values in a range on my spreadsheet are zero. Currently I am using the following code:

Dim Cell As Range
For Each Cell In Range("E17:E25")
    If Cell.Value = "0" Then
    MsgBox ("If hardware is required, please  manually populate the corresponding sections.")
    End If
Next

将显示该消息,但是该消息显示了9次(对于该范围内的每个单元格).我需要检查 E17:E25 范围内的所有值是否均为零,然后仅显示一个消息框.有什么想法吗?

The message is displayed, however it is shown 9 times (for each of the cells in the range). What I need is to check if all the values in the range E17:E25 are zero, and then display only one message box. Any ideas?

谢谢.

推荐答案

您想知道 all 的值是否为0?你可以做

You want to know if all the values are 0? You could just do

如果WorksheetFunction.Sum(Range("E17:E25"))= 0然后MsgBox(如果需要硬件,请手动填充相应的部分.")

不需要循环.

如果要检查任何其他数字,并且如果所有单元格都是该数字,则可以执行以下操作:

If you want to check for any other number, and if all cells are that number, you can do this:

Sub t()
Dim rng As Range
Dim myNum as Long
myNum = 1
Set rng = Range("B3:B6")
If WorksheetFunction.CountIf(rng, myNum) = rng.Count Then MsgBox ("All the same!")
End Sub

这篇关于检查范围内的所有值是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆