2013-12-12 50 views
-1

我試圖計算一個工作日的時間間隔,其中小時具有不同的類別/收入,並且標準時間根據日期不同而不同。我爲此使用了Excel 2010,但我希望一旦弄明白這是如何完成的,它將作爲工資系統的一部分來實施。根據滾動日期和時間間隔計算工作時間

標準小時,類別如下:

Monday-thursday: 08.00 - 14.30 Category AA 
Monday-thursday: 14:30 - 21:00 Category AB 
Monday-thursday: 21:00 - 08:00 Category AC 
Friday: 08.00 - 14:00 Category AA 
Friday: 14:00 - 21:00 Category AB 
Friday: 21.00 - 08:00 Category AC 

Sat and Sun: Category AC 

所以,我在找的是做以下一些方法。

如果員工工作在隨後的日子裏,與給定的開始,午飯時間

Day;WorkStart;WorkEnd;LunchStart;LunchEnd 
Mon;18;04;23:30;00:00 
Thu;06;15;11;11:30 
Fri;08;17;12;12:30 
Sat;05;12;09;09:30 

然後,我應該得到以下

Result: 
Day;intervalStart;IntervalEnd;Category; HoursCount 
Mon;18:00;23:30;AC;5,5;-- due to lunch being subtracted 
Mon:00:00;04:00;AC;4 
Thu;06:00;08:00;AC;2 
Thu;08:00;14:30;AA;6 --due to lunch being subtracted 
Thu;14:30;15:00;AB;0,5 
Fri;08:00;14:00;AA;5,5; --due to lunch being subtracted 
Fri;14:00;17:00;AB;3 
Sat;05:00;12:00;AC;6,5;--due to lunch being subtracted 

Mon: AC -> 9,5 
Thu: AC -> 2 
Thu: AB -> 0,5 
Thu: AA -> 2 
Fri: AA -> 5,5 
Fri: AB -> 3 
Sun: AC -> 6,5 

我試過看起來基礎上,UPS日子,然後我用大量的公式檢查所有不同的情況,但是它是不穩定的,緩慢的,如果我在小時或其他事情上做出改變,它會中斷。

我也嘗試了不同的表格變化,但由於星期五不同,我不斷得到錯誤的答案,並且必須考慮到當21:00交叉時該類別也會切換。

因爲這是間隔工作的,所以我必須計算間隔時間,而且我還沒有找到計算方法,所以我可以在一天中爲不同的間隔分配一個類別。雖然管理這個星期五是不同的,並且滾動工作日的問題。

我想弄清楚如何在公式中做到這一點,但我不排除使用VBA。理想情況下,當我有這個原型的工作時,我可以要求這是在計算工作時間和分配工資的系統中實現的。

Calculating overtime work有點類似,但沒有考慮到不同的間隔時間或滾動工作日。

我還沒有發現任何可以使用的東西。也許是因爲我對這個領域不熟悉,所以我不能把自己的手指放在正確的搜索詞上。

+0

這似乎是一個複雜的數據結構在Excel中進行處理你有沒有試過把它移到數據庫? – engineersmnky

+0

我還沒試過。我希望能在Excel中進行原型設計。我建議我可以將它移動到sqllite db。 我認爲這裏面臨的主要挑戰是如果我無法做到這一點,那麼就有可能向其他人解釋它的工作原理。開發人員不可能瞭解我們想要的是什麼。 而這次演習將毫無意義。我會嘗試將它移動到數據庫。 除了結構之外,還有關於如何計算類別的建議? – Tommy

+0

如果你想在Excel中這樣做,我會建議設置類對象和使用集合或字典結構 – engineersmnky

回答

0

這是一個非常簡單的例子,將需要用大量的額外代碼擴展到處理天翻車和許多其他的東西,但只是給你一個概念

類屬:

VERSION 1.0 CLASS 
BEGIN 
    MultiUse = -1 'True 
END 
Attribute VB_Name = "Category" 
Attribute VB_GlobalNameSpace = False 
Attribute VB_Creatable = False 
Attribute VB_PredeclaredId = False 
Attribute VB_Exposed = False 
Private pweekday As String 
Private ptime_start As Double 
Private ptime_stop As Double 
Private pcategory As String 

Public Property Let weekday(day As String) 
    pweekday = day 
End Property 
Public Property Let time_start(time As Double) 
    If checkTime(time) Then 
     ptime_start = time 
    Else 
     MsgBox "Start Error" 
    End If 
End Property 
Public Property Let time_stop(time As Double) 
    If checkTime(time) Then 
     ptime_stop = time 
    Else 
     MsgBox "Stop Error" 
    End If 
End Property 
Public Property Let category(cat As String) 
    pcategory = cat 
End Property 
Public Sub setup(cat As String, day As String, t_start As Double, t_stop As Double) 
    If checkTime(t_start) And checkTime(t_stop) Then 
     pcategory = cat 
     pweekday = day 
     ptime_start = t_start 
     ptime_stop = t_stop 
    Else 
     MsgBox "SetupError" 
    End If 
End Sub 
Public Property Get category() As String 
    category = pcategory 
End Property 
Public Property Get weekday() As String 
    weekday = pweekday 
End Property 
Public Property Get time_start() As Double 
    time_start = ptime_start 
End Property 
Public Property Get time_stop() As Double 
    time_stop = ptime_stop 
End Property 
Private Function checkTime(time As Double) As Boolean 
    checkTime = time <= 24 And time >= 0 
End Function 

類時間表

VERSION 1.0 CLASS 
BEGIN 
    MultiUse = -1 'True 
END 
Attribute VB_Name = "TimeTable" 
Attribute VB_GlobalNameSpace = False 
Attribute VB_Creatable = False 
Attribute VB_PredeclaredId = False 
Attribute VB_Exposed = False 
Private ptime_table As New Collection 

Public Sub add(cat As String, day As String, t_start As Double, t_stop As Double) 
    Dim addcat As New category 
    addcat.setup cat, day, t_start, t_stop 
    ptime_table.add addcat 
End Sub 
Public Function getStartCategory(day As String, t_start As Double, hours As Double) As String 
    Dim c As category 
    Dim c_start As String 
    Dim start_hours As Double 
    For Each c In ptime_table 
     If c.weekday = day Then 
      If t_start >= c.time_start And t_start <= c.time_stop Then 
       If hours > (c.time_stop - t_start) Then 
        start_hours = c.time_stop - t_start 
       Else 
        start_hours = hours 
       End If 
       c_start = CStr(start_hours) & ";" & c.category 
      End If 
     End If 
    Next c 
    getStartCategory = c_start 
End Function 
Public Function getStopCategory(day As String, t_stop As Double, hours As Double) As String 
    Dim c As category 
    Dim c_stop As String 
    Dim stop_hours As Double 
    For Each c In ptime_table 
     If c.weekday = day Then 
      If t_stop <= c.time_stop And t_stop >= c.time_start Then 
       If hours > (t_stop - c.time_start) Then 
        stop_hours = t_stop - c.time_start 
       Else 
        stop_hours = hours 
       End If 
       c_stop = CStr(stop_hours) & ";" & c.category 
      End If 
     End If 
    Next c 
    getStopCategory = c_stop 
End Function 
Public Function getCategory(day As String, t_start As Double, t_stop As Double) As String 
    Dim hours As Double 
    hours = hoursworked(t_start, t_stop) 
    c_start = getStartCategory(day, t_start, hours) 
    c_stop = getStopCategory(day, t_stop, hours) 
    getCategory = c_start & ";" & c_stop 
End Function 
Private Function hoursworked(t_start As Double, t_stop As Double) As Double 
    hoursworked = t_stop - t_start 
End Function 

簡單的例子模塊

Sub setup() 
    Dim tt As New TimeTable 
    With tt 
     .add "AA", "Monday", 8, 14.5 
     .add "AA", "Tuesday", 8, 14.5 
     .add "AA", "Wednesday", 8, 14.5 
     .add "AA", "Thursday", 8, 14.5 
     .add "AA", "Friday", 8, 14 
     .add "AB", "Monday", 14.5, 21 
     .add "AB", "Tuesday", 14.5, 21 
     .add "AB", "Wednesday", 14.5, 21 
     .add "AB", "Thursday", 14.5, 21 
     .add "AB", "Friday", 14, 21 
     .add "AC", "Monday", 21, 8 
     .add "AC", "Tuesday", 21, 8 
     .add "AC", "Wednesday", 21, 8 
     .add "AC", "Thursday", 21, 8 
     .add "AC", "Friday", 21, 8 
     .add "AC", "Saturday", 0, 24 
     .add "AC", "Sunday", 0, 24 
    End With 
    MsgBox tt.getCategory("Monday", 12, 18) 

End Sub 

這將返回「2.5; AA; 3.5; AB」含義星期一此人在AA類工作2.5小時,在AB類工作3.5小時。

就像我說的,這是一個如何啓動,需要更多的錯誤處理功能和數據映射一個簡單的例子,但希望這將讓你開始

+0

這是很多,謝謝。 我覺得這足以讓我開始 – Tommy