Это ни в коем случае не идеально, но это дает вам основу для работы.
Логика такова:
- использовать Коллекции для хранения ваших данных, когда вы читаете каждый столбецсоставление списка данных, относящихся к каждому пользователю.
- Создайте рабочий лист для каждого пользователя и запишите его коллекцию на соответствующий рабочий лист.
- Используйте функцию сортировки Excel, чтобы упорядочить список.
- Переместите отсортированный список на лист FinalResults.
Код выглядит следующим образом (обратите внимание, что в настоящее времячитает все столбцы, но записывает только пользователя A. Я оставлю вам право расширять код, чтобы охватить других пользователей):
Option Explicit
Sub TransferTimeCodes()
Dim UserA As New Collection
Dim UserB As New Collection
Dim UserC As New Collection
Dim sh As New Worksheet
Dim rw As Range
Dim ColCount, RowCount As Integer
Dim Msg As String
Dim WrdArray() As String
Set sh = Sheets("Timesheet")
RowCount = 0
ColCount = 1
'Loop through the Process A User column and build up list of timecodes
' for users who used Process A
For Each rw In sh.Rows
'Check for User A
If sh.Cells(rw.Row, ColCount).Value = "A" Then
'Timecode is one cell to the right
' Join the two pieces of data together to ensure alignment
'Note the "A" is fixed because it is Process A
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserA.Add Msg
End If
'Check for User B
If sh.Cells(rw.Row, ColCount).Value = "B" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserB.Add Msg
End If
'Check for User C
If sh.Cells(rw.Row, ColCount).Value = "C" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserC.Add Msg
End If
'Check for the end of the column
If sh.Cells(rw.Row, ColCount).Value = "" Then
Exit For
End If
RowCount = RowCount + 1
Next rw
RowCount = 0
ColCount = 3
'Loop through the Process B User column and build up list of timecodes
' for users who used Process B
For Each rw In sh.Rows
If sh.Cells(rw.Row, ColCount).Value = "A" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserA.Add Msg
End If
If sh.Cells(rw.Row, ColCount).Value = "B" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserB.Add Msg
End If
If sh.Cells(rw.Row, ColCount).Value = "C" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserC.Add Msg
End If
If sh.Cells(rw.Row, ColCount).Value = "" Then
Exit For
End If
RowCount = RowCount + 1
Next rw
RowCount = 0
ColCount = 5
'Loop through the Process C User column and build up list of timecodes
' for users who used Process C
For Each rw In sh.Rows
If sh.Cells(rw.Row, ColCount).Value = "A" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserA.Add Msg
End If
If sh.Cells(rw.Row, ColCount).Value = "B" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserB.Add Msg
End If
If sh.Cells(rw.Row, ColCount).Value = "C" Then
Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
UserC.Add Msg
End If
If sh.Cells(rw.Row, ColCount).Value = "" Then
Exit For
End If
RowCount = RowCount + 1
Next rw
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "UserA"
Set sh = Sheets("UserA")
For RowCount = 0 To UserA.Count - 1
WrdArray() = Split(UserA(RowCount + 1), ",")
sh.Range("A1").Offset(RowCount, 0).Value = WrdArray(0)
sh.Range("A1").Offset(RowCount, 1).Value = WrdArray(1)
Next RowCount
sh.UsedRange.Sort key1:=Range("B1"), order1:=xlAscending, Header:=xlNo
sh.UsedRange.Columns(2).Copy
Worksheets("FinalResults").Range("B2").PasteSpecial Transpose:=True
End Sub