2017-10-14 43 views
0

我正在其計算每名員工每週檢視小時的時間片之前相同。我已經實施了一些代碼,用於確定員工是否屬於公司,無薪休假,離開公司或已經離職。如何檢查是否單元格的行前是增加了反

我想要做的就是創建一個表,告訴我加入者,畢業生和按周的無薪休假的數量。

應用代碼到我的數據進行排序後,它看起來是這樣的:

Data table 在那裏我已經替換成更有意義的說明之前存在的隨機值。

我想創建一個表,增加了木匠櫃檯首次行的值未加入」所以我不重複計數英寸例如,ID 1,我不想指望那個人在周2木匠 - 5,只有在周2

這是到目前爲止我的代碼,我在移動之前在看每列行下一列:

Dim LastCol As Long 
Dim LastRow As Long 
Dim I As Long 
Dim Z As Long 
Dim Q As Long 
Dim Joined As Integer 
Dim ws As Worksheet 

'set worksheet to use 
Set ws = Sheets("Sheet1") 

With ws 
    'Find last col and row for range 
    LastCol = ws.Cells(3, Columns.Count).End(xlToLeft).Column 
    LastRow = ws.Cells(Rows.Count, D).End(xlUp).Row 

For I = 3 To LastCol 
    NotJoined = 0 
     For Z = 4 To LastRow 
      'check if cell is > 0 
      If ws.Cells(Z, I).Value > 0 Then 
       Joined = Joined + 1 
      End If 
     Next Z 
    'Find last row and add value to row below 
    Q = ws.Cells(Rows.Count, I).End(xlUp).Row 
    ws.Cells(Q + 1, I).Value = Joined 
Next I 
End With 

我將如何添加一個方法來只能算一個木匠(打算從沒有加入任何其他值)/離校生一次(從任意值X會)的任何給定行中卻發現這些價值每週。我正在朝着具有類似於下面的表:

Resulting table from data

提前感謝!

回答

1

我已經完成了一半的工作。請休息一下。這是我得到的。將代碼粘貼到標準代碼模塊中,並將工作表的名稱從「Tony」更改爲您正在測試的任何內容。

Option Explicit 

Enum Nst       ' Status 
    ' 15 Oct 2017 
    NstNone = -3 
    NstNotJoined 
    NstOnLeave 
    NstLeft 
    NstPresent 
End Enum 

Enum Nix       ' Array index 
    ' 15 Oct 2017 
    NixPresent = 1 
    NixJoined 
    NixLeft 
    NixUnpaid 
End Enum 

Sub HeadCount() 
    ' 15 Oct 2017 

    Dim Arr() As Integer    ' Result 
    Dim Wk As Long, Ix As Nix   ' Arr() indices 
    Dim Ws As Worksheet 
    Dim LastCol As Long 
    Dim LastRow As Long 
    Dim StatusArr As Variant 
    Dim R As Long, C As Long   ' Row/Column 
    Dim Stat As Nst, NewStat As Nst  ' status 

    'set worksheet to use 
    Set Ws = Sheets("Tony") 
    StatusArr = Array(NstNotJoined, NstOnLeave, NstLeft, NstPresent) 

    With Ws 
     ' prefix a period to refer to object in With statement 
     LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
     ReDim Arr(1 To (LastCol - 3), 1 To NixUnpaid) 

     For R = 4 To LastRow 
      Stat = NstNone     ' unable to determine changes in Wk 1 
      For C = 4 To LastCol 
       Wk = C - 3 
       NewStat = StatusArr(Application.Match(Val(.Cells(R, C).Value), StatusArr, 1) - 1) 
       If NewStat <> Stat Then  ' count changes 
        ' Count joiners 
        If (Stat = NstNotJoined) And (NewStat = NstPresent) Then 
         Arr(Wk, NixJoined) = Arr(Wk, NixJoined) + 1 
        End If 
        ' count leavers 
        If (Stat <> NstLeft) And (NewStat = NstLeft) Then 
         Arr(Wk, NixLeft) = Arr(Wk, NixLeft) + 1 
        End If 
       End If 

       Stat = NewStat 
       If Stat = NstOnLeave Then Arr(Wk, NixUnpaid) = Arr(Wk, NixUnpaid) + 1 
       If Stat = NstPresent Then Arr(Wk, NixPresent) = Arr(Wk, NixPresent) + 1 
      Next C 
     Next R 

     .Cells(20, "D").Resize(UBound(Arr, 2), UBound(Arr, 1)).Value = Application.Transpose(Arr) 
    End With 
End Sub 

我向您的記錄中引入了以下邏輯。

  1. -2是指 「尚未接合」
  2. -1表示 「休假」(據推測未付)
  3. 0或空白表示 「不工作」(據推測左)
  4. 任何其他正數表示工作時間(假定爲工作)

請使用工作表,而不是你目前有文本上的這些數字。如果需要,可以編寫代碼來翻譯它們。在代碼中,這些數字用Enum Nst表示。在本週開始時使用NstNone。我認爲你的系統在這一點上是有缺陷的,因爲你不知道有多少人加入或離開而不知道以前的狀態。 NstNone彌合了差距。

的最後一行代碼確定該輸出將被寫在範圍D20:在其上的數據被發現的相同片材的H23。現有數據將被覆蓋。請務必在員工ID下面的A列中填寫任何內容,因爲宏使用列A來確定要評估的員工人數。

你有以上週的結果將有儘可能多的列。它將由Enum Nix確定的4行,這意味着員工在場,加入,離開和無薪休假。這個想法是,你可以將這個列表粘貼到一個你想要的字幕和格式的表格中。您可以通過修改Enum Nix來更改序列。請務必將值0123'分配給名字。 (並且不要洗牌Enum Nst!)

我的號碼與結果中的號碼不匹配。那是因爲我不明白你的結果。但是,我認爲數字在那裏,如果您需要額外的數字,他們將很容易融入到既定的系統中。

相關問題