粘貼下面提到vba代碼在模塊中。你只需要修改變量source_rng(包含頭文件的原始數據的範圍)和target_rng(你想要粘貼結果的單元格引用)
例如,如果你的原始數據在範圍H3:m10然後source_rng =。範圍( 「H3:M10」) - 這個範圍應該包括頭也
現在要粘貼單元格 「O3」 的結果則target_rng = .Range( 「O3」)
現在。將下面提到的代碼粘貼到模塊中
Sub t()
Dim myarr()
Dim myarr_max()
Dim source_rng As Range
Dim target_rng As Range
With ActiveSheet
Set source_rng = .Range("h3:m10")
Set target_rng = .Range("o3")
target_rng.CurrentRegion.Clear
source_rng.Copy
target_rng.PasteSpecial (xlPasteAll)
Selection.Columns(2).Delete shift:=xlToLeft
.Range(Selection.Cells(2, 3), Selection.Cells(Selection.Rows.Count, Selection.Columns.Count)).ClearContents
Selection.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
For k = 1 To 3
For Each target_cell In Selection.Columns(1).Cells
i = i + 1
If i <> 1 And target_cell <> "" Then
target_count = target_count + 1
For Each source_cell In source_rng.Columns(1).Cells
j = j + 1
If j <> 1 Then
If target_cell.Value & "_" & target_cell.Offset(0, 1) = source_cell.Value & "_" & source_cell.Offset(0, 2) Then
Counter = Counter + 1
ReDim Preserve myarr(Counter - 1)
myarr(Counter - 1) = source_cell.Offset(0, k + 2)
End If
End If
Next source_cell
ReDim Preserve myarr_max(target_count - 1)
myarr_max(target_count - 1) = WorksheetFunction.Max(myarr)
Erase myarr
Counter = 0
End If
Next target_cell
.Range(.Cells(Selection.Rows(2).Row, Selection.Columns(k + 2).Column), .Cells(Selection.Rows(2).Row + UBound(myarr_max), Selection.Columns(k + 2).Column)) = WorksheetFunction.Transpose(myarr_max)
Erase myarr_max
target_count = 0
i = 0
j = 0
Next k
End With
End Sub
來源
2014-09-29 13:20:50
sam
你需要用宏來做到這一點嗎? – 2014-09-29 11:24:09