죄송합니다. 귀하의 질문에서 오해했습니다. 여기에 내가이되고 싶어보다 더 복잡한 결국 작업 코드입니다 : D
Option Explicit
Private Function inCollection(ByRef myCollection As Collection, ByRef value As Variant) As Boolean
Dim i As Integer
inCollection = False
For i = 1 To myCollection.Count
If (myCollection(i) = value) Then
inCollection = True
Exit Function
End If
Next i
End Function
Sub listMinimums()
Dim source As Range
Dim target As Range
Dim row As Range
Dim i As Integer
Dim datas As New Collection
Dim minRows As New Collection
Set source = Range("A2:C5")
Set target = Range("D2")
target.value = source.value
For Each row In source.Rows
With row.Cells(1, 1)
If (inCollection(datas, .value) = False) Then
datas.Add .value
minRows.Add row.row, .value
End If
If (Me.Cells(minRows(.value), 2) > row.Cells(1, 2)) Then
minRows.Remove (.value)
minRows.Add row.row, .value
End If
End With
Next row
'output'
For i = 1 To minRows.Count
target(i, 1) = Me.Cells(minRows(i), 1)
target(i, 2) = Me.Cells(minRows(i), 2)
target(i, 3) = Me.Cells(minRows(i), 3)
Next i
Set datas = Nothing
Set minRows = Nothing
End Sub
참고 : 시트의 이름으로 Me
을 대체 할 수 있습니다.
이 정말 Access로 데이터를 던져 쿼리를 실행하려는 것이 그 엑셀 사례 중 하나입니다. 'GroupBy'와'Min' 함수를 사용하면 원하는 것을 정확히 얻을 수 있습니다. 뭔가 'SELECT Column1, Column2, Min (Column3) As Column3 FROM Table GROUP BY Column1'과 같은 것입니다. 이 코드를 작성하는 것은 위대한 훈련이지만, Access와 같은 도구를 사용하면 이런 경우에 매우 도움이 될 수 있습니다. –
Access에 대한 필요성이 없습니다. Excel은 ADO에 상당히 만족합니다. – Fionnuala
이것은 쉽게 접근 할 수 있었지만 도구는 Excel ... ADO 샘플이 재미있어 보입니다. –