简体   繁体   English

在vba中的IsEmpty()Excel函数未按预期运行

[英]IsEmpty() Excel function in vba not operating as expected

I want create a worksheet that has a number of properties, eg columns that only accept number entries and columns that only accept values from drop down lists. 我想创建一个具有许多属性的工作表,例如只接受数字条目的列和仅接受下拉列表中的值的列。 I have created a VBA script which works almost perfectly however I have one problem I can't solve which seems to be related to the IsEmpty() function. 我已经创建了一个几乎完美的VBA脚本但是我有一个我无法解决的问题似乎与IsEmpty()函数有关。

At the moment, in columns C, D and HI can only enter values form the drop down list and my error message ("Please select value from drop-down list") is thrown otherwise (including if values are pasted in). 目前,在C列中,D和HI只能从下拉列表中输入值,否则将抛出我的错误消息(“请从下拉列表中选择值”)(否则会粘贴值)。 The IsEmpty() function call allows values to be deleted without throwing errors. IsEmpty()函数调用允许删除值而不会抛出错误。

In the Value_Columns range, again I can only enter values and these are formatted correctly. Value_Columns范围内,我再次只能输入值,并且这些值格式正确。 If I delete the value in a single cell, the IsEmpty() function behaves normally, and the value is cleared. 如果我删除单个单元格中的值,则IsEmpty()函数正常运行,并清除该值。 However, if I select a range of cells (eg I5:I10 ) and press delete, the following error message is thrown: 但是,如果我选择一系列单元格(例如I5:I10 )并按删除,则会抛出以下错误消息:

Entry must be a number 参赛作品必须是一个数字

This is in contrast to columns C, D and H, where deleting a range of cell contents throws no error message. 这与列C,D和H形成对比,其中删除一系列单元格内容不会引发错误消息。

I can't see any reason why this behaviour appears to be inconsistent. 我看不出为什么这种行为看起来不一致的原因。 Can anyone help? 有人可以帮忙吗?

Sub Worksheet_Change(ByVal Target As Range)

Dim Industry_Column As Range
Dim Proposition_Column As Range
Dim Status_Column As Range
Dim Value_Columns As Range

Set Industry_Column = Range("C5:C500")
Set Proposition_Column = Range("D5:D500")
Set Status_Column = Range("H5:H500")
Set Value_Columns = Range("I5:W500")

If Not IsEmpty(Target) Then

    If Not Application.Intersect(Target, Industry_Column) Is Nothing Then

        If IsError(Application.Match(Target, Worksheets("Drop Down Lists").Range("A2:A6"), 0)) Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "ERROR - Please select value from drop-down list"
            Application.EnableEvents = True

        End If

    ElseIf Not Application.Intersect(Target, Proposition_Column) Is Nothing Then

        If IsError(Application.Match(Target, Worksheets("Drop Down Lists").Range("C2:C6"), 0)) Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "ERROR - Please select value from drop-down list"
            Application.EnableEvents = True

        End If

    ElseIf Not Application.Intersect(Target, Status_Column) Is Nothing Then

        If IsError(Application.Match(Target, Worksheets("Drop Down Lists").Range("E2:E6"), 0)) Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "ERROR - Please select value from drop-down list"
            Application.EnableEvents = True

        End If

    ElseIf Not Application.Intersect(Target, Value_Columns) Is Nothing Then

        If Not IsNumeric(Target) Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "ERROR - Entry must be a number"
            Application.EnableEvents = True

        Else

            Target.NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "

        End If

    End If

End If

End Sub

If you look on the definition of IsEmpty() https://msdn.microsoft.com/en-us/library/office/gg264227.aspx it indicate it work on an expression 如果你查看IsEmpty()的定义https://msdn.microsoft.com/en-us/library/office/gg264227.aspx它表明它可以在表达式上工作

When you do If Not IsEmpty(Target) Then VBA implicitly cast it If Not IsEmpty(Target.Value) Then and Range.Value is an expression. 当你执行If Not IsEmpty(Target) Then VBA隐式地转换它If Not IsEmpty(Target.Value) Then和Range.Value是一个表达式。 So a standalone range can be considered an expression. 因此,独立范围可以被视为表达式。

But in your case, the implicit cast to expression don't work. 但在你的情况下,隐式转换为表达式不起作用。 You'll have to create a fonction that look if all cells are empty. 您必须创建一个查看所有单元格是否为空的函数。

Implement and replace your IsEmpty() by the following IsRangeEmpty() and it will work 通过以下IsRangeEmpty()实现并替换您的IsEmpty() ,它将起作用

' Return if a range is empty
Private Function IsRangeEmpty(ByVal rng As Range) As Boolean
    Dim cell As Range
    ' for each cell in the range
    For Each cell In rng
        ' if a cell is not empty
        If Not IsEmpty(cell) Then
            ' return "not empty"
            IsRangeEmpty = False
            Exit Function
        End If
    Next

    ' Here all cells are empty
    IsRangeEmpty = True
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM