在excel

时间:2015-07-29 20:21:20

标签: excel excel-vba vba

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

例如,以下是我的数据:

         A                                 B                            C
 Initial Meetings          Follow-up Meetings            Date
         1                                1                       7/29/2015
         0                                1                       7/30/2015
         1                                1                       7/31/2015
         0                                0                       8/1/2015
         0                                0                       8/2/2015

我想写一个公式,在一个单元格中返回“2015年7月29日,2015年7月31日”,以及“2015年7/29/2015,2015年7月30日,2015年7月31日”另一个,但我似乎陷入困境。

1 个答案:

答案 0 :(得分:0)

您无法使用vLookup执行此操作。

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

如果您对宏可以,则可以将以下内容放入新模块中,然后使用=MultiVlookup(lookup_value,table_array, col_index_num),就像使用vlookup一样,它应该给出你是一个逗号分隔的多个匹配列表:

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

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

enter image description here

最后我的原始建议(如果它帮助其他人 - 它不是问题的确切解决方案)是:

我自己没有尝试过,但是this link显示了我认为您可能正在寻找的内容。