Excel VBA - 以逗号分隔的数据排序的单元格

时间:2017-04-18 20:26:29

标签: excel vba excel-vba

我有数据:

    A          B

1  RED   him, her, kirk

2  BLUE  kirk, rose, jill

我想按B列排序,为B列中以逗号分隔的每个数据点创建行,以输出:

    C       D

1  her     RED

2  him     RED

3  jill    BLUE

4  kirk    RED

5          BLUE

6  rose    BLUE

你能帮我解决这个问题吗?我假设我需要excel来查看第2列中的每个单元格,为每个逗号分隔的数据点创建数据库,然后向每个数据库添加第1列颜色。我不知道该怎么做并输出它。

1 个答案:

答案 0 :(得分:0)

尝试在SuperUser

上阅读相同的问题

我假设您将文件插入excel:

Sub test()

Set iWsh = WorkSheets("Sheet1") 'Sheet containing raw data (text-file)
Set oWsh = Worksheets("Sheet2") 'Sheet you wanted to have processed data in it

Set rng = [B1]
Set rng = Range(rng, Cells(Rows.Count, rng.Column).End(xlUp))

 rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
                   ConsecutiveDelimiter:=False, Tab:=False, _
                   Semicolon:=False, Comma:=True, Space:=False, Other:=False

i = 1
j = 2
k = 1

  While iWsh.Cells(i,1) <> "" ' Loops through rows of the data
    Do While iWsh.Cells(i,j) <> "" 'Loops through column of the data 

       oWsh.Cells(k,1) = iWsh.Cells(i,j)

       oWsh.Cells(k,2) = iWsh.Cells(i,1)

      k = k + 1
      j = j + 1

    Loop

     i = i + 1
     j = 2

  Wend

End Sub