【问题标题】:VBA compare cell value to all values of a columnVBA将单元格值与列的所有值进行比较
【发布时间】:2018-09-21 09:47:59
【问题描述】:

我正在尝试将列 (column1) 的每个单元格与另一列 (column2) 中的所有单元格进行比较,以及该值是否存在于 column2 中。如果它存在于 column1 并且 clumn3 中的同一行声明“TRADABLE”以突出显示它。

使用此代码,它只将值与 r2 进行比较,而不是与整个 r 列进行比较,我不知道如何更改它

Sub mark_cells()


ActiveSheet.Range("D2").Select
    Do Until ActiveCell.Value = ""
        If ActiveCell.Value = Range("R2").Value And ActiveCell.Offset(0, -2).Value = "TRADABLE" Then
        ActiveCell.Interior.ColorIndex = 46


        End If

    ActiveCell.Offset(1, 0).Select

    Loop

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您可以使用工作表匹配公式来做到这一点。

    Sub mark_cells()
    
        dim i as long
    
        with activesheet
    
            for i =2 to .cells(.rows.count, "D").end(xlup).row
    
                if not iserror(application.match(.cells(i, "D").value, .range("R:R"), 0)) and _
                   ucase(.cells(i, "B").value2) = "TRADABLE" then
    
                    .cells(i, "D").Interior.ColorIndex = 46
    
                end if
            next i
    
        end with
    
    end sub
    

    更动态的方法是条件格式规则。

    Sub mark_cells()
    
        With activesheet.range(.cells(2, "D"), .cells(.rows.count, "D").end(xlup))
            .FormatConditions.Delete
            .FormatConditions.Add Type:=xlExpression, Formula1:="=and(C2="TRADABLE", isnumber(match(d2, r:r, 0)))"
            .FormatConditions(.FormatConditions.Count).Interior.ColorIndex = 46
        End With
    
    end sub
    

    【讨论】:

      【解决方案2】:

      将以下公式插入c1=if(countif(a:a;b1) >=1; "Tradable"; "") 并复制粘贴到下面的单元格中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        相关资源
        最近更新 更多