جرب
Sub ProcessData()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastRow As Long, i As Long
Dim officeName As String, dateValue As String, claimNumber As String
Dim uniqueOffices As New Collection
Dim officeDates As New Dictionary
Dim officeClaims As New Dictionary
' Set references to the worksheets
Set ws1 = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to the actual name of your worksheet
Set ws2 = ThisWorkbook.Sheets("Sheet2") ' Change "Sheet2" to the actual name of your worksheet
' Find the last row in worksheet 1
lastRow = ws1.Cells(ws1.Rows.Count, "O").End(xlUp).Row
' Loop through the data in worksheet 1
For i = 1 To lastRow
' Get the office name
officeName = ws1.Cells(i, "O").Value
' Add the office name to the uniqueOffices collection
On Error Resume Next
uniqueOffices.Add officeName, CStr(officeName)
On Error GoTo 0
' Get the date value
dateValue = CStr(ws1.Cells(i, "P").Value)
' Get the claim number
claimNumber = CStr(ws1.Cells(i, "Q").Value)
' Add the date and claim number to the dictionaries if they don't already exist
If Not officeDates.Exists(officeName) Then
officeDates.Add officeName, dateValue
officeClaims.Add officeName, claimNumber
ElseIf InStr(1, officeDates(officeName), dateValue) = 0 Then
officeDates(officeName) = officeDates(officeName) & " + " & dateValue
ElseIf InStr(1, officeClaims(officeName), claimNumber) = 0 Then
officeClaims(officeName) = officeClaims(officeName) & " + " & claimNumber
End If
Next i
' Write the unique office names to worksheet 2
Dim office As Variant
Dim rowIndex As Long: rowIndex = 1
For Each office In uniqueOffices
ws2.Cells(rowIndex, 1).Value = office
' Write the dates for each office
ws2.Cells(rowIndex, 2).Value = officeDates(office)
' Write the claim numbers for each office
ws2.Cells(rowIndex, 3).Value = officeClaims(office)
rowIndex = rowIndex + 1
Next office
MsgBox "Process complete."
End Sub
يرجى تغيير اسمي الورقتين "Sheet1" و "Sheet2" إلى الأسماء الفعلية للورقتين الخاصتين بك.