为什么尽管有“ IsEmpty”对象,此公式仍不排除空白单元格?

时间:2019-01-17 19:36:17

标签: excel vba

下面列出的公式不能给我想要的结果,因为它还会累加其中没有任何值的单元格。但是,“ IsEmpty”应该排除那些单元格吗?!

这是我的代码:

from kivy.config import Config
from kivy.core.window import Window

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

from kivy.graphics import Color, Rectangle

from kivy.app import App


class LabelWithBackground(Label):

    def __init__(self, bgcolor, **kwargs):
        super().__init__(**kwargs)
        self.bgcolor = bgcolor
        self.draw_background()

    def draw_background(self):
        if self.canvas is not None:
            self.canvas.before.clear()
            with self.canvas.before:
                Color(*self.bgcolor)
                Rectangle(pos=self.pos, size=self.size)

    def on_size(self, *args):
        self.draw_background()

    def on_pos(self, *args):
        self.size = self.texture_size
        self.draw_background()


class MyApp2(App):

    def __init__(self):

        super().__init__()

        self.layout = BoxLayout()
        self.layout.orientation = 'vertical'

        self.labels = [
            Label(text='label_0', color=(0, 0, 0, 1)),
            LabelWithBackground(text='label_1', color=(0, 0, 0, 1), size_hint=(.5, None), bgcolor=(1, .5, 0, 1)),
            Label(text='label_2', color=(0, 0, 0, 1)),
            LabelWithBackground(text='label_3', color=(0, 0, 0, 1), size_hint=(None, .25), bgcolor=(1, .5, 0, 1)),
            Label(text='label_4', color=(0, 0, 0, 1)),
            LabelWithBackground(text='label_5', color=(0, 0, 0, 1), size_hint=(None, None), bgcolor=(1, .5, 0, 1))]

        for lbl in self.labels:
            self.layout.add_widget(lbl)

    def build(self):
        return self.layout


if __name__ == '__main__':

    Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
    Window.clearcolor = (1, 1, 1, 1)

    MyApp2().run()

也可以在这里找到工作表的屏幕截图: enter image description here

范围G6:G9是粘贴公式的单元格。例如:单元格G6应该说2,而不是1。

感谢您的帮助,我已经尝试了一切,但真的迷路了!

1 个答案:

答案 0 :(得分:0)

我无法使用IsEmpty重现该问题,正如您在尝试过的简约代码中所看到的那样,一切正常。因此,我想问题出在您在函数范围之外执行的某些代码中。

Function ScopeSum(rng As Range) As Double
    Dim cell As Range
    Dim cumulative As Double

    For Each cell In rng.Cells
        If Not IsEmpty(cell.Value) Then
            cumulative = cumulative + cell.Value - 1
        End If
    Next cell

    ScopeSum = cumulative
End Function

enter image description here

而且,不会很简单

=SUM(C3:F3)-COUNTA(C3:F3)

每行都一样吗?