Results 1 to 3 of 3

Do While Loops with the conditions being time and date

Threaded View

  1. #1
    Registered User
    Join Date
    04-07-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2010
    Posts
    1

    Do While Loops with the conditions being time and date

    Hi Guys,
    Well, I am kind very new to VBA so the code I have is very inefficient. I am trying to use VBA to create a table documenting the number of entries I have given a certain date and time. More specifically, I am trying to find the number of entries I have for each day starting with the end of business (5:00 pm) the previous business day to the end of business on the current day. I attached an example of my code and raw data for the first week of January 2013. What I need is a much more efficient way of doing what I am trying to do.

    Thanks!
    Sub test()
    Range("f2").Activate
    x = ActiveCell.Value
    
    Do While x = DateValue("31-Dec-12")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 2).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 2).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    
    Do While x = DateValue("01-Jan-13")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 2).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 2).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    Worksheets("Sheet2").Range("b1") = "=SUM(Sheet1!h:h)"
    
    Do While x = DateValue("02-Jan-13")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 3).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 3).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    Worksheets("Sheet2").Range("b2") = "=SUM(Sheet1!i:i)"
    
    
    
    Do While x = DateValue("03-Jan-13")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 4).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 4).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    Worksheets("Sheet2").Range("b3") = "=SUM(Sheet1!j:j)"
    
    Do While x = DateValue("04-Jan-13")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 5).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 5).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    
    
    Do While x = DateValue("05-Jan-13")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 5).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 5).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    
    Do While x = DateValue("06-Jan-13")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 5).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 5).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    Worksheets("Sheet2").Range("b4") = "=SUM(Sheet1!k:k)"
    
    Do While x = DateValue("07-Jan-13")
    If ActiveCell.Offset(0, 1) > TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 6).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    Loop
    
    Do Until ActiveCell.Offset(0, 1).Value > TimeValue("5:00:00 PM")
    If ActiveCell.Offset(0, 1) <= TimeValue("5:00:00 PM") Then
    ActiveCell.Offset(0, 6).Value = 1
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    Else
    ActiveCell.Offset(1, 0).Activate
    x = ActiveCell.Value
    End If
    
    Loop
    Worksheets("Sheet2").Range("b5") = "=SUM(Sheet1!l:l)"
    
    End Sub
    Attached Files Attached Files
    Last edited by newskooldemz; 04-07-2013 at 07:14 AM. Reason: Forgot to add attachment

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