+ Reply to Thread
Results 1 to 14 of 14

how to solve this problem

  1. #1
    Registered User
    Join Date
    09-12-2006
    Posts
    13

    how to solve this problem

    hi there.
    I have a problem to solve and hope You could help me. I have to create a macro which will count cells with date and hour enteries from 00:00 to 07:00 and 20:00 to 00:00. the enteries are in YYYY-MM-DD HH:MM:SS format. theres also the problem of saturdays and sundays where all 'actions' in those days count. I have an idea how to do it but don't know how to realize it.
    d=2006-01-02 00:00:00//start date

    for z=1 to 5 //to know which day of the week it is

    do

    d+z=b
    z=z+1

    countif b > 00:00 and < 07:00
    and b > 20:00 and < 00:00

    for z = 6 to 7
    do
    count all enteries
    end
    Please help.

  2. #2
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    Can you tell us with column or range the date times are in and where you want to total the number of 00:00 to 07:00 and 20:00 to 00:00 entries
    VBA - The Power Behind the Grid

    Posting a sample of your workbook makes it easier to look at the Issue.

  3. #3
    Registered User
    Join Date
    09-12-2006
    Posts
    13
    the date and time enteries are in the E column. the sum, well, the W column would be just fine

  4. #4
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    when you say
    theres also the problem of saturdays and sundays where all 'actions' in those days count

    do you mean:
    you want all entires for the 48 hours for of saturday or sunday..
    Or all entries in the times 00:00 to 07:00 and 20:00 to 00:00. FOr Sat/Sun
    Tell me if this will be ok:
    This will can be the output:
    Column W ( description) and U ( counts)
    rows 1-4

    Business weekday 00:00 to 07:00 Count1
    Business weekday 20:00 to 00:00. Count2
    Saturday 00:00- 07:00 & 20:00-00:00 Count3
    Sunday 00:00- 07:00 & 20:00-00:00 Count 4

  5. #5
    Registered User
    Join Date
    09-12-2006
    Posts
    13
    with the Sat/Sun problem I ment the 48h because if it was the 00:00 to 07:00 and 20:00 to 00:00 there would bo no problem to find out which day of the week it was. I just need the sum for the business week and Sat/Sun separate. only the W and the next columns are free to use.

    Thanks for helping me out with this.

  6. #6
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    Try this:

    Sub countshifts()
    'Column W(Description) And X(counts)
    'Rows 1 - 3

    'Business weekday 00:00 to 07:00 Count1
    'Business weekday 20:00 to 00:00 Count2
    'Sat & Sun Count3
    Dim DateofEntry As String
    Dim TimeofEntry As String
    Dim conventry As String
    Dim count1 As Integer
    Dim count2 As Integer
    Dim count3 As Integer
    ' Zero counts
    count1 = 0
    count2 = 0
    count3 = 0


    For Each cell In Sheets("Sheet1").Range("D1", Range("A65536").End(xlUp).Address)
    'Note: format of cell YYYY-MM-DD HH:MM:SS

    If cell.Value <> "" Then
    conventry = Format(cell.Value, "YYYY-MM-DD HH:MM:SS")
    DateofEntry = Left(conventry, 10)
    TimeofEntry = Replace((Right(conventry, 8)), ":", "")


    If Weekday(DateofEntry, vbMonday) < 6 Then
    If TimeofEntry >= 0 And TimeofEntry <= 70000 Then count1 = count1 + 1
    If TimeofEntry > 200000 And TimeofEntry <= 239999 Then count2 = count2 + 1

    Else
    count3 = count3 + 1

    End If

    Else
    End If

    Next cell

    With Sheets("Sheet1")
    .Range("W1").Value = "Business weekday 00:00 to 07:00"
    .Range("W2").Value = "Business weekday 20:00 to 00:00"
    .Range("W3").Value = "Sat & Sun "
    .Range("X1").Value = count1 'Business weekday 00:00 to 07:00
    .Range("X2").Value = count2 'Business weekday 20:00 to 00:00
    .Range("X3").Value = count3 'Sat & Sun
    End With
    End Sub

  7. #7
    Registered User
    Join Date
    09-12-2006
    Posts
    13
    well, i forgot to write, my sheets names are: GDA, POZ, POZN, WAR, KATN, but i guess that something like UserSheet = AcitveSheet should solve it.in general the macro dosen't work. After I change the "Sheet1" into "POZ" in the line For Each cell In Sheets("Sheet1").Range("D1", Range("A65536").End(xlUp).Address) and in With Sheets("POZ") i get an runtime error '13' - type mismatch in the line If Weekday(DateofEntry, vbMonday) < 6 Then.
    Please excuse my ignorance in this field that may cause missunderstanding. I really appriciate Your help.

  8. #8
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    sorry found a typo in my code

    change:
    Sheets("Sheet1").Range("D1", Range("A65536").End(xlUp).Address) to
    Sheets("Sheet1").Range("D1", Range("D65536").End(xlUp).Address)

    next problem...
    First thing is to ensure your worksheets are exactly the spelling and case of as there are referred to in the code ( or vice versa)

    do you have the cells in col D formatted int the custom format:
    YYYY-MM-DD HH:MM:SS
    ( I am not sure if this really makes much difference though)

    If you want you can email me the xls and I will have a look it to see where the drama is.

    In the meantime I added some error checking and mods to the code.
    The results appear on sheet one..but now it searches all sheets.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-12-2006
    Posts
    13
    I'll send You my .xls in a moment

  10. #10
    Registered User
    Join Date
    09-12-2006
    Posts
    13
    I've recieved your xls but when I trieid to run it, I.ve got the '13' error... but I took my time today and modified your code a little bit and came out with this:
    Sub statistics()
    'Column W(Description) And X(counts)
    'Rows 1 - 3
    'Business weekday 00:00 - 07:00 i 20:00 do 24:00 Count1
    'Sat/Sun Count2
    'not important Count3

    Dim DateofEntry As String
    Dim TimeofEntry As String
    Dim conventry As String
    Dim count1 As Integer
    Dim count2 As Integer
    Dim count3 As Integer
    Dim WS As Worksheet
    ' Zero counts
    count1 = 0
    count2 = 0
    count3 = 0
    On Error GoTo errhandle
    For Each WS In Worksheets
    count1 = 0
    count2 = 0
    count3 = 0


    For Each cell In WS.Range("E2", Range("E65536").End(xlUp).Address)
    'Note: format of cell YYYY-MM-DD HH:MM:SS

    If cell.Value <> "" And Len(cell.Value) > 0 Then

    conventry = Format(cell.Value, "yyyy-mm-dd hh:mm:ss")

    DateofEntry = Left(conventry, 19)
    TimeofEntry = Replace((Right(conventry, 9)), ":", "")

    If Weekday(DateofEntry, vbMonday) < 6 And TimeofEntry >= 0 And TimeofEntry <= 70000 Then count1 = count1 + 1


    Else: count3 = count2 + count1

    End If
    Next cell


    For Each cell In WS.Range("E2", Range("E65536").End(xlUp).Address)
    'Note: format of cell YYYY-MM-DD HH:MM:SS

    If cell.Value <> "" And Len(cell.Value) > 0 Then

    conventry = Format(cell.Value, "yyyy-mm-dd hh:mm:ss")

    DateofEntry = Left(conventry, 19)
    TimeofEntry = Replace((Right(conventry, 9)), ":", "")

    If Weekday(DateofEntry, vbMonday) < 6 And TimeofEntry >= 200000 And TimeofEntry <= 239999 Then count1 = count1 + 1

    Else: count3 = count2 + count1

    End If
    Next cell


    For Each cell In WS.Range("E2", Range("E65536").End(xlUp).Address)
    'Note: format of cell YYYY-MM-DD HH:MM:SS

    If cell.Value <> "" And Len(cell.Value) > 0 Then

    conventry = Format(cell.Value, "yyyy-mm-dd hh:mm:ss")

    DateofEntry = Left(conventry, 19)
    TimeofEntry = Replace((Right(conventry, 9)), ":", "")

    If Weekday(DateofEntry, vbMonday) >= 6 Then count2 = count2 + 1

    Else: count3 = count2 + count1

    End If

    Next cell


    With WS
    .Range("W3").Value = "Business weekday"
    .Range("W4").Value = "Sat & Sun"
    .Range("X2").Value = "il"
    .Range("X3").Value = count1 'Business weekday
    .Range("X4").Value = count2 'Sat & Sun
    End With
    Next WS
    Exit Sub

    errhandle:
    If Err.Number = 13 Then
    MsgBox " Sprawdż format komórek: RRRR-MM-DD HH:MM:SS"
    Err.Clear
    End If


    End Sub
    and it works. altough I'm thinking to optimize it a little. additionally I'll have to make this macro to get data from another Sheet.

  11. #11
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    ok...this is find you just plit the if's out into two different loops
    As you probably know
    This code does all sheets..and put the counts on each sheet in col X

    but it does not provide a summary count of all sheets.

  12. #12
    Registered User
    Join Date
    09-12-2006
    Posts
    13
    i didn't get the meaning of your first sentence
    ok...this is find you just plit the if's out into two different loops
    I'm not English/American and I don't understand everything. I didn't need the summary count, this code does great! Thanks for all Your help!

  13. #13
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    sorry my typo's

    ...this is fine you just split the if's out into two different loops

    that is this is fine you separatedd the IF statements for the two time periods into 2 different loops

  14. #14
    Registered User
    Join Date
    09-12-2006
    Posts
    13
    I had to split the if's because that was the only way the macro worked properly... I tried variuos combinations and non of them were good. I took me some time, after all I'm a complete VB novice.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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