你可以使用一個For循環和分配活動(和/或任何其他相關信息)到特定於人員的陣列。然後使用該數組填充電子郵件。
這不是很優雅,但它運行在您提供的示例數據上。您可以使用數組數據(並遍歷項目),也可以使用逗號分隔的字符串爲玩家填充電子郵件。
Public Sub ReportToPeople()
Dim i As Integer, j As Integer
Dim arrPlayerX()
Dim arrPlayerY()
Dim arrPlayerZ()
Dim strPlayerX As String
Dim strPlayerY As String
Dim strPlayerZ As String
ReDim arrPlayerX(1 To 1)
ReDim arrPlayerY(1 To 1)
ReDim arrPlayerZ(1 To 1)
' No clue how large your dataset is...
For i = 2 To 100
If Cells(i, 1).Value = vbNullString Then Exit For
Select Case Cells(i, 2).Value
Case "Player X"
Call fArrIncrementAndAdd(arrPlayerX, Cells(i, 3).Value, strPlayerX)
Case "Player Y"
Call fArrIncrementAndAdd(arrPlayerY, Cells(i, 3).Value, strPlayerY)
Case "Player Z"
Call fArrIncrementAndAdd(arrPlayerZ, Cells(i, 3).Value, strPlayerZ)
End Select
Next i
Call MsgBox(strPlayerX) 'or use Debug.Print strPlayerX
End Sub
Public Function fArrIncrementAndAdd(ByRef arrTheArray, ByVal strValueToAdd, ByRef strWholeString)
Dim c As Integer
c = UBound(arrTheArray)
If arrTheArray(c) <> vbNullString Then ReDim Preserve arrTheArray(1 To (c + 1))
arrTheArray(c) = strValueToAdd
strWholeString = strWholeString & "," & strValueToAdd
If UBound(arrTheArray) = 1 Then strWholeString = Replace(strWholeString, ",", "") ' removes leading comma on first item
End Function
無需VBA就可以了。嘗試'SUMIF'公式並隱藏您的原始數據表。如果你真的需要它隱藏起來,你可以用密碼來保護工作簿,這樣用戶就不能取消隱藏,或者至少你可以通過將VBA設置爲'xlVeryHidden'來使用Visible'屬性。 –
如果您已經擁有代碼,請將其發佈 - 修改它比從頭開始編寫代碼更容易;) – Jook