简体   繁体   中英

Find cell with max value of column, copy entire row and paste in row 2

I have searched the web for an answer for this but can't translate the answers I find into exactly what I want to do.

I want to find the cell in Range("L5:L" & LastRow) with maximum value, and copy that entire row and paste it on row 2.

I know the code to get the maximum value into cell L4. But I also want to copy the row which the maximum value is in. That code is below. But how to select and copy the entire row? Seems like such a simple thing, and I can't get it to work.

So this is what I have to get the value:

Range("L4").Value = Application.WorksheetFunction.max(Range("L5:L" & LastRow))

The following will find the max value, find the first row with the max value, and take row values from the max value row and populate row 2 with the results.

Dim lngRow As Long
Dim lngMax As Long
Dim rngTemp As Range
Dim rngCell As Range
Dim lastRow As Long

lastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set rngTemp = Range("L5:L" & lastRow)
lngMax = Application.WorksheetFunction.Max(Range("L5:L" & lastRow))

For Each rngCell In rngTemp
    If rngCell.Value = lngMax Then
        lngRow = rngCell.Row
        Exit For
    End If
Next rngCell

Rows(2).Value = Rows(lngRow).Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM