首页 文章

Excel - 如果一系列单元格等于列表中的值,我想填充另一个单元格的值

提问于
浏览
-1

我正在尝试编写一个公式,如果一系列单元格等于列表中特定的值,则该公式将返回一个单元格的值 .

例如:

A1 = Orange
    B1:B5 = My Data 
    If B1:B5 = Red, Blue, or Green, display the value from A1

谢谢你的帮助!

2 回答

  • 0
    Dim a as Range, listValues() as string 'array of string
    Dim str as string, i as integer
    
    
    str = "Red;Blue;Green"
    listValues = Split(str,";") 'split str using ";"
    for i= Lbound(listValues) to Ubound(listValues) 'loop thru the array
        Set a = ActiveSheet.UsedRange.Find(what:=listValues(i), lookat:=xlWhole) 
        if Not a Is Nothing Then
            'value found
            a.value = Range("A1").value
        end if
    next i
    

    我've adapted @Davesexcel '的决议,检查一下:How to avoid using select in VBA for variable cell ranges?

    RonDeBruin的这个决议也很好:

    http://www.rondebruin.nl/win/s9/win006.htm

  • 0

    将您的值放在列中的某个位置:

    enter image description here

    那么C1中的一个简单公式:

    =IF(ISNUMBER(MATCH(B1,E:E,0)),$A$1,"")
    

    如果找到匹配,则返回其行号 . If测试它是数字还是错误 . 如果是数字,则找到匹配并返回A1中的值 . 否则返回一个空字符串 .

    enter image description here

相关问题