Last active
December 20, 2015 00:28
-
-
Save bmcbride/6041645 to your computer and use it in GitHub Desktop.
Excel macro to redistribute a delimited column of data into separate rows (keeping other data as is). Modification of http://www.excelfox.com/forum/f22/redistribute-a-delimited-column-of-data-into-separate-rows-keeping-other-data-as-is-420/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub RedistributeData() | |
Dim X As Long, LastRow As Long, A As Range, Table As Range, Data() As String | |
Const Delimiter As String = ", " | |
Const DelimitedColumn As String = "B" | |
Const TableColumns As String = "A:B" | |
Const StartRow As Long = 2 | |
Application.ScreenUpdating = False | |
LastRow = Columns(TableColumns).Find(What:="*", SearchOrder:=xlRows, _ | |
SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row | |
For X = LastRow To StartRow Step -1 | |
Data = Split(Cells(X, DelimitedColumn), Delimiter) | |
If UBound(Data) > 0 Then | |
Intersect(Rows(X + 1), Columns(TableColumns)).Resize(UBound(Data)).Insert xlShiftDown | |
End If | |
If Len(Cells(X, DelimitedColumn)) Then | |
Cells(X, DelimitedColumn).Resize(UBound(Data) + 1) = WorksheetFunction.Transpose(Data) | |
End If | |
Next | |
LastRow = Cells(Rows.Count, DelimitedColumn).End(xlUp).Row | |
On Error Resume Next | |
Set Table = Intersect(Columns(TableColumns), Rows(StartRow).Resize(LastRow - StartRow + 1)) | |
If Err.Number = 0 Then | |
Table.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C" | |
Columns(DelimitedColumn).SpecialCells(xlFormulas).Clear | |
Table.Value = Table.Value | |
End If | |
On Error GoTo 0 | |
Application.ScreenUpdating = True | |
End Sub |
bmcbride,
Thanks for this useful post. I was able to get the column with delimited values separated into rows by each delimited value. However, I was unable to get the data from the other columns to repeat for each individual record I had just created with the individual delimited values. Any advice on how to achieve this? My dataset has 18 columns (columns A:R).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For use with Fulcrum data extracts- Column A should contain fulcrum_id and column B should contain comma separated list of photos. Firs row should contain header fields (fulcrum_id, photos).