【问题标题】:Lookup value in merged cell在合并单元格中查找值
【发布时间】:2014-07-22 19:35:22
【问题描述】:

我在使用 Excel 时遇到问题。

我有一个表,其中 A 列中的值对应于 B 列中的多个值。就像 A 在合并的单元格 A1:A6 中包含汽车公司,B 在属于该的单个单元格中包含 6 个汽车名称公司。

现在我必须使用 Car Company 作为 LookUp 值执行 VLookup,然后返回属于该公司的所有汽车名称。

问题是当我执行 VLookUp 时,返回的是第一个单元格中的汽车名称,而不是所有 6 辆汽车。

抱歉,我无法在此处添加任何 excel 图片,因为我在 stackoverflow 上的声誉还不够。

【问题讨论】:

  • 您不能使用 VLOOKUP 来执行此操作。
  • 谢谢@DavidZemens,请问有什么建议或替代方法吗?
  • 考虑一个用户定义的函数。
  • @DavidZemens 任何小例子都会有很大帮助... :-(

标签: excel merge vlookup cells


【解决方案1】:

把它放在你工作簿的普通代码模块中:

Function GetModelNames(make As String, columnRange As Range, columnOffset As Integer)
Dim r As Long
Dim mergeRange As Range
Dim cl As Range

Dim ret
'Gets the first ROW within specified columnRange
r = Application.Match(make, columnRange, False)

'iterate over the mergeArea
For Each cl In columnRange(r).MergeArea
    'Concatenate a string of each cell in the offset column:
    ret = ret & IIf(ret = "", "", ", ") & cl.Offset(, columnOffset).Value

Next

'return the value to the function
GetModelNames = ret
End Function

然后这样称呼它:

=GetModelNames("Ford", A:A, 1)

论据:

  • make: 应该是整车厂家
  • columnRange:应该是一个 byRef 范围地址,例如“A1:A500”或“A:A”等。
  • columnOffset:离 columnRange 多少列?

【讨论】:

  • 干杯。如果这解决了您的问题,请将答案标记为“已接受”
猜你喜欢
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 2018-12-13
相关资源
最近更新 更多