[英]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 . 实际上,我已经通过Google学习开始VBA编程,并制作了一些宏来使我的工作自动化。
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: 我要在一个范围内比较值,并且每行中的较高值都想在F列中打印:
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. 您可以使用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: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.