简体   繁体   English

我想比较3个不同的列(B,C,D)的值,然后想在F列中获得较高的值结果

[英]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.

相关问题 我有一个 Excel 表,它有 3 列 AB C 列,我希望 D 列是 RGB(A,B,C) 的颜色 - I have an Excel sheet that has 3 columns A B C columns and I want the D column to be the color of RGB(A,B,C) Excel:我想用B列的内容填充A列,只有当B列值以F开头时 - Excel: I want to populate Column A with the Contents of Column B, only if Column B value starts with F 我想将 if 条件放在两列上,如果结果为真,我想从不同的工作表中查找值 - I want to put the if condition on two columns and if the result is true, i want to vlookup the value from a different sheet 比较两列 A 和 B 以获取 3 列 C、D 和 E 中的数据 - Compare two columns A and B to get data in 3 columns, C,D & E 我想在 D 列中找到平均值 - I want to find the average value in column D 如何比较两列匹配和不匹配的值,并以所需的格式获取它? - How do I compare two columns for matching and non matching values and get it in the format that I want? 在 Excel 中,我想将每个单元格值与该列中单元格值的 rest 进行比较 - In Excel, I want to compare each cell value with the rest of the cell values in that column 我想使此代码适用于两个变量,它只复制B列中的单元格A1的数据,我希望它也记录D列中的单元格C1的数据? - I want to make this code to work for two variables it only copies data of cell A1 in column B i want it to record data of cell C1 in column D as well? 如果 B 列存在,则比较 C 列中的值 - compare values in column C if column B exists 我想比较两列并复制单元格 - I want to compare two columns and copy cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM