首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VBA检查整个范围是否有有效日期而不是空白?

VBA检查整个范围是否有有效日期而不是空白?
EN

Stack Overflow用户
提问于 2017-04-06 18:56:07
回答 1查看 1.5K关注 0票数 2
代码语言:javascript
运行
复制
I have a workbook like so:

Dates
01/02/2017
01/03/2017
BLANK
01/02/2017

我正在尝试运行一个宏,但前提是我范围内的所有单元格都是有效的日期,而不是空的。

我使用的是以下内容:

代码语言:javascript
运行
复制
Dim cell As Range

    'With my workbook, lets check the data
    With wb.Worksheets(1)
    Lastrow = .Cells(.Rows.count, "G").End(xlUp).Row

    'Data Check: Are all dates valid?
    For Each cell In Range("E9:E" & Lastrow)
    If IsDate(cell.Value) And Not IsEmpty(cell.Value) Then
    Continue
    Else
    Exit Sub

    End If
    Next
    End With

但这不管用。无论发生什么,宏仍然运行!如果重要,本专栏中的单元格是数据验证列表。

有人能告诉我我哪里出了问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-06 19:15:33

稍微颠倒一下逻辑:

代码语言:javascript
运行
复制
    Dim cell As Range

    'With my workbook, lets check the data
    With wb.Worksheets(1)
        Lastrow = .Cells(.Rows.Count, "G").End(xlUp).Row

        'Data Check: Are all dates valid?
        For Each cell In Range("E9:E" & Lastrow)
            If Not IsDate(cell.Value) Or Trim(cell.Value) = "" Then
                Exit Sub
            End If
        Next
        ' the rest of your code.
        ' it will not get here if there are any blanks in or non dates in the range.

    End With
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43263618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档