Reputation: 196
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:
would be transformed to
How would I realise that within excel? Any clues? Thanks in advance.
Upvotes: 1
Views: 1278
Reputation: 59495
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))
Upvotes: 1
Reputation: 1506
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.
Upvotes: -1
Reputation: 5100
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
Upvotes: 3
Reputation: 41
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.
Upvotes: 3