+ Reply to Thread
Results 1 to 1 of 1

B5 uses VBA Code. How to properly display info in B5 into AB5?

Hybrid View

jonvanwyk B5 uses VBA Code. How to... 11-19-2010, 01:54 PM
  1. #1
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Question B5 uses VBA Code. How to properly display info in B5 into AB5?

    Cell B5 uses the following VBA:

    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim TimeStr As String
    
    On Error GoTo EndMacro
    If Application.Intersect(Target, Range("B5:B12,D5:D12,F5:F12,H5:H12,J5:J12,L5:L12,N5:N12,B16:B23,D16:D23,F16:F23,H16:H23,J16:J23,L16:L23,N16:N23")) Is Nothing Then
        Exit Sub
    End If
    If Target.Cells.Count > 1 Then
        Exit Sub
    End If
    If Target.Value = "" Then
        Exit Sub
    End If
    
    Application.EnableEvents = False
    With Target
    If .HasFormula = False Then
        Select Case Len(.Value)
            Case 1 ' e.g., 0001 = 00:01 AM
                TimeStr = "00:0" & .Value
            Case 2 ' e.g., 0012 = 00:12 AM
                TimeStr = "00:" & .Value
            Case 3 ' e.g., 0735 = 7:35 AM
                TimeStr = Left(.Value, 1) & ":" & _
                Right(.Value, 2)
            Case 4 ' e.g., 1234 = 12:34
                TimeStr = Left(.Value, 2) & ":" & _
                Right(.Value, 2)
    
            Case Else
                Err.Raise 0
        End Select
        .Value = TimeValue(TimeStr)
    End If
    End With
    Application.EnableEvents = True
    Exit Sub
    EndMacro:
    
    Application.EnableEvents = True
    End Sub

    The VBA code the represents cell B5 allows a user to type in military time without the use of a colon, and automatically converts it into a real time. For instance 0700 is converted into 07:00 as a time of day. When i type 0700 into B5, the VBA works perfectly, converting it into 07:00.

    Cell AB5 points to cell B5 using: =B5

    Cell AB5 (using =B5) does not display 07:00. Instead it shows "0". When 1500 is typed into cell B6, it correctly converts to 15:00; but displays "1" in cell AB6, which is pointed at cell B6 (using =B6).

    I have been trying to create an electronic timesheet for our organization for some time. With all the caveats that goes into our shedules, it is taking about 5,000 formulas, but I am VERY close to a working prototype! Thank you in advance to anyone who is able to help.
    Last edited by jonvanwyk; 11-19-2010 at 03:44 PM. Reason: More consice title.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1