Excel “UnCONCATENATE”/explode in function / Convert cell into array

前端 未结 2 1907
不思量自难忘°
不思量自难忘° 2021-01-07 06:38

I am trying to \"Unconcatenate\" a string in Excel 2010. Yes, I know that is not a real word. So pretty much; I have a cell that can not be split into multiple columns the c

相关标签:
2条回答
  • 2021-01-07 06:58

    The following code exposes VBA's Split function for worksheet use--it returns a row array of items that have been split using a specified delimiter. For example, if cell A1 contained the text "Item 1,Item 2"), EXPLODE(A1,",") would return an array with elements "Item 1" and "Item 2".

      Function EXPLODE(str As String, Optional delimiter As Variant) As Variant
          If IsMissing(delimiter) Then
              delimiter = " "
          End If
          EXPLODE = Split(str, delimiter)
      End Function 
    

    It is an array function. To use the returned elements in the spreadsheet:

    • Select the cells in which you want the "exploded" items show
    • Enter the function specifying the cell with the source string (or reference to the cell which contains the source) and the delimiter on which the split will be done
    • Complete the entry using the Control-Shift-Enter key combination.

    Alternatively, individual elements can be chosen using the INDEX function--=INDEX(EXPLODE(A1,1,2) would return "Item 2" using the previous example. (Given a range or array, the INDEX function returns the value in the ith row and jth column.) This usage does not require the formula to be entered as an array formula.

    For your use case, a combination with other functions would be in order. You have a string with multiple items of the form "aa, bb, cc" (the result of a VLOOKUP) and want to determine whether any of the elements of this string can be found as individual items in any of the cells in column A. You want a function that will return True if all of the elements are found, and False otherwise. The following formula achieves that result:

      =SUM(SIGN(IFERROR(MATCH(TRIM(EXPLODE(D1,",")),$A:$A,0),0)))=COUNTA(EXPLODE(D1,","))
    

    It is an array formula and needs to be entered with Control-Shift-Enter. Note that I used the contents of cell D1 in lieu of your lookup value. The TRIM function strips out any extraneous spaces between the elements of the string with multiple items.

    0 讨论(0)
  • 2021-01-07 07:15

    (not really an answer, just trying to figure out what the question is)

    Sheet 1 has a dropdown box with a number of items in, the selected item is used in a vlookup() looking at a table in sheet 2.

    Sheet 2 has 2(+) columns, one which is an index used for a vlookup and the other which contains delimited lists.

    Sheet 3 has 1(+) columns, each row has a value that may correspond to an item in one of the delimited lists in sheet 2.

    When an item is selected in the dropdown box on sheet 1 I want to lookup the corresponding list in sheet 2 (using the vlookup) and then see if any of the items in that list exist in sheet 3.

    Is this what your trying to do? If yes, what is the result of this search?

    • Boolean: True-Some matches found!, False-No Matches
    • Number: I found this many results

    No? :(

    Update

    Doing that with just worksheet functions would be fairly tricky!

    VBA is a much better choice. (the final step atleast)

    Add the following code to a new module (not a worksheet or workbook module) and it will be available as a UDF on your worksheets.

    This function takes a string (which is your delimited list) it is exploded inside the function so you dont have to worry about doing it.

    I've not tested it, but in theory it should allow you to pass it a list. The function then should check sheet 3 for you and return true/false depending on weather the items are found or not.

    I know that you've found an answer, but here is a working and slightly faster version of my function.

    Public Function ValidateList(ByVal Target As Range) As Boolean
    Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Sheet3") ' Check Sheet3 is the correct sheet
    Dim List As Variant: List = Sheet.UsedRange.Columns("A").Value2       ' Set Column(A) to correct column
    Dim Items() As String: Items = Split(Target.Value2, ",")
    Dim Item As Long
    Dim Key As String
    Dim Result As Boolean: Result = False
    Dim Search As Object: Set Search = CreateObject("Scripting.Dictionary")
    
    For Item = LBound(Items) To UBound(Items)
        Search.Add Trim(Items(Item)), False
    Next Item
    
    If Search.Count > 0 Then
        ' Target List has 1+ Items
        For Item = LBound(List, 1) To UBound(List, 1)
            Key = Trim(List(Item, 1))
            If Search.Exists(Key) = True Then
                Search.Remove Key
            End If
            If Search.Count = 0 Then
                Result = True
                Exit For
            End If
        Next Item
    Else
        ' Target List is Empty
        ' Optionally set result to True here if empty list should return True
        ' Defaults to False
    End If
    
    ValidateList = Result
    
    End Function
    
    0 讨论(0)
提交回复
热议问题