Excel解析包含逗号分隔列表的单元格,并将每个值用作索引


-2

Excel Sheet1,ColumnA为

1) 3, 2, 4
2) 1

数字指的是Sheet2,ColumnB中的行号,其中包含标识符

1) foo
2) bar
3) eggs
4) sausage

我最后应该看到的是工作表1中的一列,其中包含

1) eggs, bar, sausage
2) foo

我很确定我可以使用Python脚本来执行此操作,但是最好在Excel中执行。不幸的是,我几乎不了解VBA。

文本到列将不会:您无法定位到新工作表,但是Sheet1ColumnA中的单元格可以包含任意数量的项目。

编辑:VLOOKUP在这里将无法使用,因为它是通过单元格内容查找的。在这里,我想通过cell reference查找。如果可以让INDEX在单个单元格中迭代许多用逗号分隔的值,INDEX会这样做。我需要首先解析单元格中的值,然后使用某种INDEX来查找每个值的对应查找值。


@Tyson Vlookup在这里不起作用,请参见编辑。这也不是一个作业问题。这是一个工作问题,分解为基础。
RolfBly

Answers:


2

此VBA代码可以解决问题:

Public Function mysearch(value As String)
    Dim myrows() As String
    myrows = Split(value, ",")
    result = ""
    For i = LBound(myrows) To UBound(myrows)
        a = ThisWorkbook.Worksheets(2).Cells(myrows(i), 2) 'The first 2 is the number of sheet and the last 2 is the column.
        result = result & a & ","
    Next i
    result = Left(result, Len(result) - 1)
    mysearch = result
End Function

使用ALT+ 转到宏/ VBA F11。在ThisWorkbook下插入一个模块,然后将代码粘贴到右侧。

如果您的参考数据在Sheet1的A列中,则放在单元格B1中=mysearch(A1),如果数据在Sheet2的B列中,您将得到结果。


谢谢!没错 令人困惑的是,工作表可以称为sheet1,但编号为sheet3,但是现在我明白了,它就像一个吊饰一样工作。再次非常感谢!
RolfBly
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.