我有一個列包含部門和另一列包含應用程序,可以,不顯示。我想計算每個部門爲Appt,Can和No Show發生多少次。我目前使用的代碼提取部門的唯一值,並使用If語句計算Appt,Can和No Show的值。查找號碼。時間價值與其他組合發生
數據集: http://bit.ly/1HkvAxR 代碼來獲得獨特部門:
Public Sub Getting_Unique_Departments()
Dim X
Dim objDict As Object
Dim lngRow As Long
If Len("E") > 0 And Len("Y") > 0 Then
Set objDict = CreateObject("Scripting.Dictionary")
X = Application.Transpose(Range("E" & 2, Cells(Rows.Count, "E").End(xlUp)))
For lngRow = 1 To UBound(X, 1)
objDict(X(lngRow)) = 1
Next
Range("Y" & 2 & ":" & "Y" & objDict.Count + 1) = Application.Transpose(objDict.keys)
End If
End Sub
代碼檢查聘任,可以,走路,沒有顯示各個部門。
Sub Calculation()
nName0 = "Department"
nName1 = "Appt"
nName2 = "Walk"
nName3 = "Can"
nName4 = "No Show"
Cells(1, 25).Value = nName0
Cells(1, 26).Value = nName1
Cells(1, 27).Value = nName2
Cells(1, 28).Value = nName3
Cells(1, 29).Value = nName4
For Dept_Row_number = 2 To Dept_lastRow
'Dept_lastRow finds last Row of unique department listed in Y col and Sheet_lastRow finds the last Row of input data sheet.
nCount1 = 0
nCount1 = 0
nCount2 = 0
nCount3 = 0
nCount4 = 0
Row_number = 1
search_string1 = ActiveSheet.Cells(Dept_Row_number, 25)
Do
DoEvents
Row_number = Row_number + 1
item_in_review1 = ActiveSheet.Cells(Row_number, 5).Value
item_in_review2 = ActiveSheet.Cells(Row_number, 3).Value
If InStr(item_in_review1, search_string1) > 0 And InStr(item_in_review2, "Appt") > 0 Then
nCount1 = nCount1 + 1
ElseIf InStr(item_in_review1, search_string1) > 0 And InStr(item_in_review2, "Walk") > 0 Then
nCount2 = nCount2 + 1
ElseIf InStr(item_in_review1, search_string1) > 0 And InStr(item_in_review2, "Can") > 0 Then
nCount3 = nCount3 + 1
ElseIf InStr(item_in_review1, search_string1) > 0 And InStr(item_in_review2, "No Show") > 0 Then
nCount4 = nCount4 + 1
End If
Loop Until Row_number = Sheet_lastRow
Cells(Dept_Row_number, 26).Value = nCount1
Cells(Dept_Row_number, 27).Value = nCount2
Cells(Dept_Row_number, 28).Value = nCount3
Cells(Dept_Row_number, 29).Value = nCount4
Next
是否有任何簡單的方法來解決這個問題,因爲萬一我必須爲多個列執行此操作時,代碼會太麻煩。
你試過了一個數據透視表嗎?這是彙總唯一值數據的標準方法。如果需要,可以使用VBA創建數據透視表。 –
我已經使用了Pivot Table,但由於某種原因,我只能通過VBA來完成。 –
'僅通過VBA執行'...在VBA中,您允許和不允許執行哪些操作?您可以在VBA中創建數據透視表,獲取所有摘要數字,在某處報告並刪除數據透視表。 –