Transforming multiple entries in a cell into multi

2019-01-29 03:28发布

I have an excel file with the names of bands and their genre.

Column A: bandname. Column B: genre.

The b column might contain multiple entries such as "pop/rock". I would want to split that into two rows.

Example:

  • [1] Awesomeband | Pop/Rock

would be transformed to

  • [1] Awesomeband | Pop
  • [2] Awesomeband | Rock

How would I realise that within excel? Any clues? Thanks in advance.

4条回答
Explosion°爆炸
2楼-- · 2019-01-29 03:41

I am assuming that Awesomeband|Pop|Rock stored at A1 place
For Awesomeband|Pop =LEFT(A1,15)
For Awesomeband|Rock ==LEFT(A1,12)&""&(RIGHT(A1,4))
I think this should work.

查看更多
不美不萌又怎样
3楼-- · 2019-01-29 03:49

For the sake of @Navneet's attempts, to split to C1:D2 put in =A$1 and copy to A2. In D1:

=LEFT(B1,FIND("/",B1)-1)  

and in D2:

=MID(B1,FIND("/",B1)+1,LEN(B1))
查看更多
三岁会撩人
4楼-- · 2019-01-29 03:51

Highlighting Column B and running this VBA should do the trick:

Sub bandsplit()
Dim c As Range
Dim splitv() As String

For Each c In Selection
    splitv = Split(c.Value, "/")
    If UBound(splitv) > 0 Then
        c.EntireRow.Insert
        c.Offset(-1, 0).Value = splitv(1)
        c.Offset(-1, -1).Value = c.Offset(0, -1).Value
        c.Value = splitv(0)
    End If
Next c
End Sub

It's restricted to 2 genres but you could hack it to add more.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-29 04:02

Had a go at writing something like @John Dirk Morrison, allows for more than two genres. doesn't work with the selection though, just column A and B on Sheet1.

Sub BandSplit()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Sheet1")
Dim Genre() As String
Dim Count As Integer
Dim Index As Integer
Dim First As Boolean
Dim Row As Integer: Row = 2 ' Exclude Header Row

Do
    Genre = Split(Sheet.Cells(Row, 2), "/")
    Count = UBound(Genre)
    First = True
    If Count > 0 Then
        Index = 0
        Do
            If First = True Then
                Sheet.Cells(Row, 2).Value2 = Genre(Index)
                First = False
            Else
                Sheet.Rows(Row).EntireRow.Insert (xlShiftDown)
                Sheet.Cells(Row, 1).Value2 = Sheet.Cells(Row + 1, 1).Value2
                Sheet.Cells(Row, 2).Value2 = Genre(Index)
            End If
            Index = Index + 1
            If Index > Count Then
                Exit Do
            End If
        Loop
    End If
    Row = Row + 1
    If Sheet.Cells(Row, 1).Value2 = "" Then
        Exit Do
    End If
Loop
End Sub
查看更多
登录 后发表回答