使用VBA检查下面的单元格是否为空 [英] Using VBA to check if below cell is empty

查看:162
本文介绍了使用VBA检查下面的单元格是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Excel中使用VBA检查以下单元格是否为空?
我想把特定范围内的所有值加起来,但只有下面的单元格不为空。

How do I use VBA in Excel to check if a below cell is empty or not? I want to sum all values in a specific range, but only, if the below cell is not empty.

VBA或其他任何一种方式?

Is that somehow possible with VBA or any other way?

示例:

4 2 3 2 1
2   3 1

总和为:4 + 3 + 2 = 9。

Sum would be: 4 + 3 + 2 = 9.

推荐答案

我会推荐一个公式为这个

I would recommend a Formula for this

公式

=SUMPRODUCT((A1:E1)*(A2:E2<>""))

SNAPSHOT

如果你还想要VBA,那么

If you still want VBA then

VBA

Option Explicit

Sub Sample()
    Dim rng As Range
    Dim Cl As Range
    Dim tot As Long

    Set rng = Range("A1:F1")

    For Each Cl In rng
        If Len(Trim(Cl.Offset(1))) <> 0 Then tot = tot + Cl.Value
    Next

    Debug.Print tot
End Sub

其实你可以在VBA中有很多版本。您也可以评估上述公式。例如

In fact you can have many versions in VBA. You can evaluate the above formula as well. For example

Option Explicit

Sub Sample()
    Debug.Print Evaluate("=SUMPRODUCT((A1:E1)*(A2:E2<>""""))")
End Sub

这篇关于使用VBA检查下面的单元格是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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