在Excel中使用Vlookup返回多个值 [英] Returning multiple values using Vlookup in excel

查看:56
本文介绍了在Excel中使用Vlookup返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个Excel工作表,可以每天自动计算一周中的每一天的会议.我想写一个公式来返回我安排的会议的所有日期(最好用逗号分隔),但是我遇到了一些困难.使用Vlookup,我只能让它返回第一个日期.

例如,这是我的数据:

  A B C首次会议后续会议日期1 1 7/29/20150 1 7/30/20151 1 7/31/20150 0 8/1/20150 0 8/2/2015 

我想写一个公式在一个单元格中返回"7/29/2015,7/31/2015",并在其中返回"7/29/2015,7/30/2015,7/31/2015"另一个,但我似乎被困住了.

解决方案

vLookup无法做到这一点.

这可以在VB脚本中相对容易地完成,但是会影响可移植性,因为如果不是大多数用户默认情况下禁用宏,许多情况下会被禁止,并且在许多情况下,由于其公司禁用了宏并使其成为用户的政策,因此禁止使用宏不应使用它们.

如果您对宏的使用感到满意,则可以将以下内容放入新模块中,然后以与以下相同的方式使用 = MultiVlookup(lookup_value,table_array,col_index_num)您将使用vlookup,它应该为您提供多个匹配项的逗号分隔列表:

 公共函数MultiVlookup(find_value,search_range,return_row)Dim myval'表示返回值的字符串(以逗号分隔的列表)昏暗逗号'布尔表示我们是否需要在下一个结果前加上,"逗号=错误'Debug.Print find_value.value,return_row对于search_range.Rows中的每个rw'遍历范围中的每一行如果rw.Cells(1,1).value = find_value然后'如果我们找到了查找值...如果是逗号,则'如果不是我们要添加到列表中的第一个值,请添加一个逗号myval = myval +,"别的逗号=真万一myval = myval + Str(rw.Cells(1,return_row).value)万一下一个MultiVlookup = myval结束功能 

这可能不是最干净的方法,它也不是vlookup的直接副本(例如,它不像vlookup那样具有第四个范围查找"参数),但是它适用于我的测试:

最后,我最初的建议(以防其他人使用-这不是问题的确切解决方案)是

我没有亲自尝试过,但是

You can't do this with vLookup.

This can be done relatively easily in a VB script, but it would affect portability as many if not most users disable macros by default and in many cases users are prevented from using Macros because their company disables them and makes it policy that users should not use them.

If you are OK with Macros, you can put the following into a new module and then use =MultiVlookup(lookup_value,table_array, col_index_num) in the same way as you'd use vlookup and it should give you a comma separated list of multiple matches:

Public Function MultiVlookup(find_value, search_range, return_row)
  Dim myval ' String to represent return value (comma-separated list)
  Dim comma ' Bool to represent whether we need to prefix the next result with ", "
  comma = False
  'Debug.Print find_value.value, return_row
  For Each rw In search_range.Rows ' Iterate through each row in the range
    If rw.Cells(1, 1).value = find_value Then ' If we have found the lookup value...
      If comma Then  ' Add a comma if it's not the first value we're adding to the list
        myval = myval + ", "
      Else
        comma = True
      End If
      myval = myval + Str(rw.Cells(1, return_row).value)
    End If
  Next
  MultiVlookup = myval
End Function

This may not be the cleanest way of doing it, and it isn't a direct copy of vlookup (for instance it does not have a fourth "range lookup" argument as vlookup does), but it works for my test:

Finally my original suggestion (in case it helps others - it's not the exact solution to the question) was:

I've not tried it myself, but this link shows what I think you might be looking for.

这篇关于在Excel中使用Vlookup返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆