使用不带日期/时间格式的Excel VBA检查时间

时间:2018-10-25 08:46:54

标签: excel vba excel-vba

我试图确保该工具的用户仅插入有效的时间值。此输入必须是数字,而不是日期时间格式。我知道前端的条件格式要简单得多,但是复制粘贴操作将覆盖条件格式。因此,我决定使用以下逻辑为其编写宏: 1)输入必须为数字格式“ 0000” 2)前2位数字必须小于23 AND 3)最后两位数必须小于59

这是我到目前为止的代码,但是没有提供预期的输出。错误在哪里或有解决此问题的更有效方法?

Worksheets("SheetA").Range("A2:A" & lastRow).NumberFormat = "0000"

For count = 2 To (lastRow + 1)
If IsEmpty(Worksheets("SheetA").Range("A" & count).Value) = True Then
    Worksheets("SheetA").Range("A" & count).Interior.Color = RGB(255, 255, 204)
Else
    If (Left(Worksheets("SheetA").Range("A" & count).Text, 2)) > 23 Or (Right(Worksheets("SheetA").Range("A" & count).Text, 2)) > 59 Then
        Worksheets("SheetA").Range("A" & count).Interior.Color = RGB(255, 0, 0)
    End If
End If
Next count 

2 个答案:

答案 0 :(得分:2)

在普通模块中,创建一个过程以将您的电话号码格式化为文本并测试其是否在所需范围内:

Public Sub TestNumber(Target As Range)

    Dim FormattedNumber As String

    'Return formatted "text" version of value.
    FormattedNumber = Format(Target, "0000")

    'Assume the value is correct and set the cell to "valid" colour.
    Target.Interior.Color = RGB(255, 255, 204)

    'Validate the value - check it's length, that it's a number
    'and falls with the specified ranges.
    If Len(FormattedNumber) = 4 And IsNumeric(Val(FormattedNumber)) Then
        If Val(Left(FormattedNumber, 2)) < 0 Or Val(Left(FormattedNumber, 2)) > 23 Or _
         Val(Right(FormattedNumber, 2)) < 0 Or Val(Right(FormattedNumber, 2)) > 59 Then
            Target.Interior.Color = RGB(255, 0, 0)
        Else
            'Remove if you don't want to reformat the number.
            Target.Value = Format(FormattedNumber, "00:00")
        End If
    Else
        Target.Interior.Color = RGB(255, 0, 0)
    End If

End Sub

要检查第1列中的数字的每张纸上,添加以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rCell As Range

    If Not Intersect(Target, Columns(1)) Is Nothing Then
        Application.EnableEvents = False
            For Each rCell In Target
                TestNumber rCell
            Next rCell
        Application.EnableEvents = True
    End If

End Sub

只有在Application.EnableEvents过程末尾重新格式化输入的数字时,才需要TestNumber行。不添加命令将再次使用重新格式化的数字触发Change事件-在再次触发并最终返回00:00之前,该事件将返回错误的结果。

修改:
已更新Change事件,因此,如果更改了多个单元格(如果粘贴了一系列数字),并且在{{1中将AND更改为OR,它将可以正常工作}}程序(以前接受 6000 )。

再次编辑:
如果要检查输入的数字,请删除TestNumber变量,并替换为FormattedNumber并将列设置为文本格式,以免丢失前导0。

答案 1 :(得分:0)

左侧功能(工作表/ VBA)

Excel LEFT函数既可以用作工作表函数,也可以用作VBA函数。 LEFT函数从第一个或最左边的字符开始,在文本字符串中返回指定数量的字符。使用此功能可从文本字符串的左侧提取子字符串。语法:LEFT(文字字符串,字符编号)。有必要提及text_string参数,这是您要从中提取指定数量的字符的文本字符串。 char_numbers参数是可选的(当用作工作表函数时),它指定要从文本字符串中提取的字符数。 char_numbers值应等于或大于零;如果它大于文本字符串的长度,则LEFT函数将完整返回文本字符串;如果省略,它将默认为1。在用作VBA函数时,必须同时指定两个参数,并且如果text_string包含Null,则该函数还将返回Null。

如果您想实现自己的目标,我可以为您提供替代方案: 您输入的内容必须是字符串,如果您控制的是时间,则必须将字符串转换为数字,否则无法进行对话。

在数字示例中转换字符串:

Dim finalNumber As Integer
    If IsNumeric(myVar) Then
        finalNumber = CInt(myVar)
    Else
        finalNumber = 0
    End If

,并限制此步骤的输入 https://www.extendoffice.com/documents/excel/952-excel-cell-character-limit.html