這是一個基於VBA的解決方案,構建了以下內容:
Option Explicit
Sub FindMaximums()
Dim DataSheet As Worksheet, MaxSheet As Worksheet
Dim LastDataRow As Long, GroupCol As Long, _
MeasureCol As Long, CurrentGroup As Long, _
MaxMeasureRow As Long, RowIdx As Long, _
LastMaxRow As Long
Dim MaxMeasureValue As Double
Dim DataRng As Range, MaxRng As Range
'identify sheets and columns for easy reference
Set DataSheet = ThisWorkbook.Worksheets("Data")
Set MaxSheet = ThisWorkbook.Worksheets("Maximums")
GroupCol = 1 'grouping info in column A
MeasureCol = 5 'measure for comparison in column E
'identify the last row to set bounds on our loop
LastDataRow = DataSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'initialize group and max variables
CurrentGroup = 1
MaxMeasureValue = 0
MaxMeasureRow = 1
LastMaxRow = 1
'loop through all rows
For RowIdx = 2 To LastDataRow
'check to see if there has been a group change
If DataSheet.Cells(RowIdx, GroupCol).Value > CurrentGroup Then
'write out the max measure row
Set DataRng = Range(DataSheet.Cells(MaxMeasureRow, GroupCol), DataSheet.Cells(MaxMeasureRow, MeasureCol))
Set MaxRng = Range(MaxSheet.Cells(LastMaxRow, GroupCol), MaxSheet.Cells(LastMaxRow, MeasureCol))
DataRng.Copy MaxRng
'initialize and increment
CurrentGroup = DataSheet.Cells(RowIdx, GroupCol).Value
MaxMeasureValue = 0
MaxMeasureRow = 1
LastMaxRow = LastMaxRow + 1
End If
'evaluate max measure logic
If DataSheet.Cells(RowIdx, MeasureCol).Value > MaxMeasureValue Then
MaxMeasureValue = DataSheet.Cells(RowIdx, MeasureCol).Value
MaxMeasureRow = RowIdx
End If
Next RowIdx
'write out the last maximums
Set DataRng = Range(DataSheet.Cells(MaxMeasureRow, GroupCol), DataSheet.Cells(MaxMeasureRow, MeasureCol))
Set MaxRng = Range(MaxSheet.Cells(LastMaxRow, GroupCol), MaxSheet.Cells(LastMaxRow, MeasureCol))
DataRng.Copy MaxRng
End Sub
'請提出一個solution' - 你嘗試過什麼?詢問代碼的問題必須證明對所解決問題的最小理解。包括嘗試解決方案,爲什麼他們沒有工作,以及預期的結果。 –
Dan Wagner使用VBA給出的正確答案。 – user3494938