Options Explicit下多列自动插入冒号(:)

Automatically inserting Colon (:) in multiple columns under the Options Explicit

我正在寻找一个代码来自动将“:”(冒号)插入到列 R 和 S、W 和 X 中,并找到了我认为可以根据需要自定义的代码,但我面临两个问题:

  1. 代码在 R 和 S 中有效,但还需要在 W 和 X 列中 运行 的代码

  2. 我得到一个错误:

    Variable not Defined - stopping at TLen and I guess it will also stop at TimeV

程序员不使用 Option Explicit,(没有 Option Explicit 也能正常工作)。但是我所有的代码总是带有 Option Explicit,但我不确定如何为两个变量编写 Dim

这段代码在特定的工作表中,在 Worksheet_Change 子项中,我还有其他代码用于其他事情,例如人们从中进行选择时的时间戳B 列,当在 B 列中进行选择时,它将自动填充。

我在另一个工作簿中尝试了冒号代码,但没有 Option Explicit 并且它可以正常工作而不会出错。

代码来源来自

Excel VBA tips n tricks #12 no more colons when typing time of day, type 123 instead of 01colon23 AM

我已调整代码以引用下面代码中的列 R 和 S。

Private Sub Worksheet_Change(ByVal Target As Range)

    ' This code will ADD the COLON for TIME automatically
    ' The code is from: https://www.youtube.com/watch?v=ATxaNbTV2d0 (Excel is Fun -
    ' Excel VBA Tips n Tricks #12 NO MORE COLONS When Typing Time of Day, Type 123 instead of 01colon23 AM

    ' To avoid an error if you select more than 1 cell, this next line of code will exit the sub

    If Selection.Count > 1 Then
        Exit Sub
    End If

    If Not Intersect(Range("R4:S1200"), Target) Is Nothing Then

        TLen = Len(Target)
        [![Layout of Worksheet and sample of the columns that need automatic insertion of colons ][1]][1]
        If TLen = 1 Then
            TimeV = TimeValue(Target & ":00")

        ElseIf TLen = 2 Then
            TimeV = TimeValue(Target & ":00")

        ElseIf TLen = 3 Then
            TimeV = TimeValue(Left(Target, 1) & ":" & Right(Target, 2))

        ElseIf TLen = 4 Then
            TimeV = TimeValue(Left(Target, 2) & ":" & Right(Target, 2))

        ElseIf TLen > 4 Then
            'Do nothing

        End If

        'Target.NumberFormat = "HH:MM"
        Application.EnableEvents = False

        Target = TimeV
        Application.EnableEvents = True

    End If
End Sub

扩大相交的范围Intersect(Range("R:S,W:X"),Target)

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub
    If IsNumeric(Target) = False Then
        MsgBox Target & " is not a number", vbExclamation
        Exit Sub
    ElseIf Intersect(Range("R:S,W:X"), Target) Is Nothing Then
        Exit Sub
    End If
    
    Dim n As Long
    n = Len(Target)
    If n >= 1 And n <= 4 Then
        Application.EnableEvents = False
        Target.NumberFormat = "hh:mm"
        If n <= 2 Then
            Target.Value2 = TimeSerial(Target, 0, 0)
        Else
            Target.Value2 = TimeSerial(Int(Target / 100), Target Mod 100, 0)
        End If
        Application.EnableEvents = True
     End If
End Sub

I understand that you are 'stretching & teaching' me to work things out for myself, and it is appreciative (and I definitely have learned how to see the type (1.)). But in this instance, the 'Type' is coming as Variant/Date, even though it is meant to be time (maybe I am misunderstanding the syntax). – TheShyButterfly

你做得很好!是的,这是找到类型的一种方法。另一种方法是使用 VarType function:

Option Explicit

Sub Sample()
    Dim TimeA

    TimeA = TimeValue("01:00 PM")

    MsgBox VarType(TimeA)
End Sub

这会给你 7vbDate

您还可以将时间存储为 VariantDouble,如下所示。

Option Explicit

Sub Sample()
    Dim TimeA As Date
    Dim TimeB As Double
    Dim TimeC As Variant

    TimeA = TimeValue("01:00 PM")
    TimeB = TimeValue("01:00 PM")
    TimeC = TimeValue("01:00 PM")

    MsgBox "Time stored as Date : " & TimeA
    MsgBox "Time stored as Double : " & TimeB
    MsgBox "Time stored as Variant : " & TimeC

    MsgBox "TimeA formated as Date : " & Format(TimeA, "hh:mm:ss AM/PM")
    MsgBox "TimeB formated as Date : " & Format(TimeB, "hh:mm:ss AM/PM")
    MsgBox "TimeC formated as Date : " & Format(TimeC, "hh:mm:ss AM/PM")
End Sub

but without an example how am I to learn, I have obviously exhausted my search on resolving this, but found nothing .. the reason why I posted the question. Thank you for encouraging me to continue solving things on my own :) TheShyButterfly

你可以把范围写成CDP1802显示在他的post或者你可以使用Application.Union method (Excel).

例如,

Option Explicit

Sub Sample()
    Dim rngA As Range
    Dim rngB As Range
    Dim rngCombined As Range

    Set rngA = Range("R4:S1200")
    Set rngB = Range("W4:X1200")

    Set rngCombined = Union(rngA, rngB)

    MsgBox rngCombined.Address
End Sub

因此在您的代码中它变为 Intersect(rngCombined, Target) Is Nothing

此外,由于您正在使用 Worksheet_ChangeEvents,我建议您查看 Working with Worksheet_Change