![Как получить значения ячеек на основе соответствующих критериев в Excel?](https://rvso.com/image/1518622/%D0%9A%D0%B0%D0%BA%20%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%20%D1%8F%D1%87%D0%B5%D0%B5%D0%BA%20%D0%BD%D0%B0%20%D0%BE%D1%81%D0%BD%D0%BE%D0%B2%D0%B5%20%D1%81%D0%BE%D0%BE%D1%82%D0%B2%D0%B5%D1%82%D1%81%D1%82%D0%B2%D1%83%D1%8E%D1%89%D0%B8%D1%85%20%D0%BA%D1%80%D0%B8%D1%82%D0%B5%D1%80%D0%B8%D0%B5%D0%B2%20%D0%B2%20Excel%3F.png)
У меня есть столбец AC в файле Excel, как показано на рисунке. Моя проблема заключается в создании таблицы, подобной той, что указана в столбце EG. Как это сделать в Excel?
решение1
Приведенный ниже макрос генерирует выходные данные в форме, показанной на прикрепленном изображении.
'assumptions
'(1) the input data is in columns A,B,C
'(2) the output data is rendered in columns E,F,G
Sub ListByTeacher()
Dim noOfRows As Integer
Dim i As Integer
Dim j As Integer
Dim c_lv_array(1) As String ' c - course, lv - load value
Dim c As New Collection 'output table with values
Dim k As New Collection 'keys
Dim tmpVar As Variant
'[INPUT]
'identify no of rows with data
i = 1
Do While Len(Cells(i, 1).Value) > 0
i = i + 1
Loop
noOfRows = i - 1
'Loop through input data rows and build the collection of teachers
For i = 2 To noOfRows
c_lv_array(0) = Trim(CStr(Cells(i, 1).Value))
c_lv_array(1) = Trim(CStr(Cells(i, 2).Value))
On Error Resume Next
tmpVar = c.Item(Trim(CStr(Cells(i, 3).Value))) 'if teacher in collection
If IsArray(tmpVar) = True Then 'exists
If InStr(1, tmpVar(0), c_lv_array(0), vbBinaryCompare) = 0 Then
'c_lv_array(0) = c_lv_array(0) & ", " & tmpVar(0) 'use this line or below one
c_lv_array(0) = tmpVar(0) & ", " & c_lv_array(0) 'reversed order to the above line
c_lv_array(1) = CStr(CInt(c_lv_array(1)) + CInt(tmpVar(1)))
End If
c.Remove Trim(CStr(Cells(i, 3).Value))
c.Add Item:=c_lv_array, Key:=Trim(CStr(Cells(i, 3).Value))
End If
tmpVar = Empty
c.Add Item:=c_lv_array, Key:=Trim(CStr(Cells(i, 3).Value))
k.Add Item:=Trim(CStr(Cells(i, 3).Value)), Key:=Trim(CStr(Cells(i, 3).Value))
Next i
'[OUTPUT]
'Render the result in s/s
Cells(1, 5) = "Teacher"
Cells(1, 6) = "Courses"
Cells(1, 7) = "Load"
j = 2
For i = 1 To c.Count
tmpVar = c.Item(k.Item(i))
Cells(j, 5) = k.Item(i)
Cells(j, 6) = tmpVar(0)
Cells(j, 7) = tmpVar(1)
j = j + 1
Next i
End Sub
решение2
Вам действительно следует сделать для этого сводную таблицу.
Таким образом, вы сможете дополнять список в столбце AC, постоянно обновляя другую точку зрения в EG.
Однако для этого необходимы две вещи:
- Чтобы иметь возможность непрерывно обновлять значения в сводной таблице, столбцы A–C следует преобразовать в таблицу (
CTRL + L
). - Сводная таблица будет иметь свой собственный лист.