简体   繁体   中英

I want to compare 3 different columns (B,C,D) values then want to get the higher value result in F column

Actually I have started VBA programming by google learning and making some macros to automate my work .
There is a range in which I want to compare values and whatever value is higher in each rows-columns want to get print in F column:

Dim cell As Range
Dim filrange As Range 
Dim lastRow As Long

lastRow = Range("A1").CurrentRegion.Rows.Count

Set filrange = Range("B2:B" & lastRow)

For Each cell In filrange.Cells.SpecialCells(xlCellTypeVisible)

  If cell.Value >= 10 And 
     (Cells(cell.Row, "C").Value) >= 10 And 
     (Cells(cell.Row, "D").Value) >= 10 Then

    cell.Offset(0, 3).Select

 End If
Next

You can do this using a Do Loop.

lastRow = Range("B1").CurrentRegion.Rows.Count 'the last row in your data in column B as I thought you referenced B, C and D
x = 1 'the starting row

Do While x <= lastRow
cells(x, 6).formula = "=Max(B" & x & ":D" & x & ")"
y = cells(x, 6)
cells(x, 6) = y
x = x + 1
Loop
cell.Offset(0, 3) = Application.WorksheetFunction.Max(Range(cell.Row, "B"),Cells(cell.Row, "D")))

You could try:

Option Explicit

Sub test()

    Dim i As Long, LastRow As Long

    With ThisWorkbook.Worksheets("Sheet1") 'Change sheet name if needed
        'Find the last row of columnA
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        'Loop column F from 1 to LastRow
        For i = 1 To LastRow
            .Range("F" & i).Value = Application.Max(.Range("B" & i).Value, .Range("C" & i).Value, .Range("D" & i).Value)
        Next i

    End With

End Sub

Results:

在此处输入图片说明

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