Excel VBA – 用逗号分隔的数据进行sorting的单元格

我有数据:

AB 1 RED him, her, kirk 2 BLUE kirk, rose, jill 

我想按B列进行sorting,为B列中每个数据点(用逗号分隔)创build行,输出:

  CD 1 her RED 2 him RED 3 jill BLUE 4 kirk RED 5 BLUE 6 rose BLUE 

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

尝试在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