自动格式化比赛结果时间

时间:2015-05-07 06:58:14

标签: excel vba excel-vba

我已经开始创建一个VBA宏,它可以帮助我添加格式到行,因为我使用自定义的NumberFormat添加它们。但是,因为我和我的伙伴有时输入类似" ss.00"这搞砸了牢房。所以我开始写另一个Sub来检查它是否缺少分号。如何添加" 0:"在这个值的前面自动显示单元格显示" 0:50.20",每当我这样做时,它最终都是一个长数字。

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Cells.Count = 1 Then
        If Target.Column = 1 Then
            If Target.Row < 24 And Target.Row > 1 Then
                Set FirstRow = Target.Offset(0, 1)
                Set LastRow = Target.Offset(0, 11)
                If Target.Value <> "" Then
                    For Each Cel In Range(FirstRow, LastRow)
                        Cel.NumberFormat = "m:ss.00;@"
                    Next
                Else
                    If MsgBox("This will erase the row! Are you sure?", vbYesNo) = vbNo Then
                        Exit Sub
                    Else
                        For Each Cel In Range(FirstRow, LastRow)
                            Cel.ClearContents
                        Next
                    End If
                End If
            End If
        End If

        Const sCheckAddress As String = "B2:L24"
        Dim rngIntersect As Range

            On Error Resume Next
            Set rngIntersect = Intersect(Me.Range(sCheckAddress), Target)
            On Error GoTo 0

            If Not (rngIntersect Is Nothing) Then
                If Target.Value2 <> "" Then
                    If InStr(Target.Value2, ":") < 1 Then

                    End If
                End If
            End If
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

也许这会对你有所帮助:

If InStr(Target.Value2, ":") < 1 Then
    ' ":" not found
    Target.Value = CStr("0:" & Target.Value)
Else
    ' ":" found
    'Nothing to add
End If