Excel-숫자 항목을 시간 값으로 변환 때문입니다. 이것은 혼란없이 작동합니다. 내

이 사용자 지정 유형으로 Excel 2010에서 일부 셀의 서식을 지정했습니다.

##":"##

사용자가 “1345”를 입력하여 “13:45″또는 “923”을 얻어 “9:23″을 얻을 수 있기를 원하기 때문입니다. 이것은 혼란없이 작동합니다.

내 문제는 특정 시나리오에서 사용자가 “13”만 입력 할 수 있으며 “-1″과 같은 이상한 결과를 제공한다는 것입니다.

위에서 언급 한 형식을 유지하면서 “13”이 “13:00″으로 형식화되도록 셀을 형식화 할 수 있습니까?

감사.



답변

“13”> “-1″시나리오를 다시 만들 수 없습니다. 지역 설정은 무엇입니까?

그러나 사용자 정의 형식은 자정과 오전 1시 사이의 시간을 처리하지 않습니다. 013의 선행 0이 제거됩니다.

이것의 문제점은 13을 입력 한 사용자가 00:13 또는 13:00을 의미하는지 알 수 없다는 것입니다. 이것이 해석에 열려있는 한 서식, 수식 또는 코드 솔루션이 도움이되지 않습니다.

이것은 다른 무엇보다 사용자 교육에 관한 것입니다.

사용자 지정 형식 대신 VBA를 적용하여

a) 사용자는 시간 값을 의심없이 해석하기에 충분한 문자를 입력합니다 (예 : 0:13은 013, 13:00은 1300).

b) 값은 시간처럼 보이도록 형식화 될뿐 아니라 실제로 시간 차이 계산에 사용할 수있는 시간 값이됩니다.

c) 시간이 입력 될 때 선행 0이 제거되지 않습니다.

다음은 이러한 항목을 날짜 / 시간 값으로 바꾸는 UDF입니다. 또한 항목에 하나 이상의 + 또는-부호를 추가하여 요일을 더하거나 빼는 기능도 포함합니다. 워크 시트 변경 이벤트에서이 함수를 호출하십시오.

Public Function TimeEntry(iTarget As String) As Variant

' convert values into date/times
'
' expected user input in the format of
'
' 1430      will be converted to today, 14:30
' 1430+     will be converted to today + 1 day, 14:30
' 1430-     will be converted to today - 1 day, 14:30
'
' multiple + or - signs are allowed to enable quick entry of time several days ago or
' in the future
'

Dim IncDay As Integer, DecDay As Integer
Dim eTime As Variant
On Error GoTo Message
    Msg = ""
    eTime = Application.WorksheetFunction.Substitute(iTarget, "+", "")
    eTime = Application.WorksheetFunction.Substitute(eTime, "-", "")
    eTime = Format(eTime, "0000")
' a few error checks to validate the data
' - can only start with a number
' - must be a number after stripping off the + and - signs
' - cannot be less than 3 or more than 4 digits
' - cannot be more than 23:59
    If Not IsNumeric(Left(iTarget, 1)) Or _
        Not IsNumeric(eTime) Or _
        Len(eTime) > 4 Or _
        eTime > 2359 Then
        GoTo Message
    End If
' insert a colon before the last two digits and convert into a time
    eTime = Left(eTime, Len(eTime) - 2) & ":" & Right(eTime, 2)
    eTime = TimeValue(eTime)
' determine how many days to increase or decrease
    IncDay = Len(iTarget) - Len(Application.WorksheetFunction.Substitute(iTarget, "+", ""))
    DecDay = Len(iTarget) - Len(Application.WorksheetFunction.Substitute(iTarget, "-", ""))

' increase/decrease current date and add the time value
    TimeEntry = Date + IncDay + (DecDay * -1) + eTime

GoTo Ende
Message:
        Msg = "Invalid time value entered" & Chr(10) & Chr(10)
        Msg = Msg & "Please enter time values like this: " & Chr(10) & Chr(10)
        Msg = Msg & " 900   for 9:00 am today " & Chr(10)
        Msg = Msg & "2130+  for 21:30 tomorrow " & Chr(10)
        Msg = Msg & " 000+  for midnight tonight" & Chr(10)
        Msg = Msg & "1000-- for 10 am two days ago."
        MsgBox Msg
        TimeEntry = ""

Ende:
End Function


답변