I'm trying to apply this macro to a spreadsheet that I have, but I'm having some issues. I keep getting a run time error. I've attached the file so that you can see what I'm working on.
Thanks for any help that you can lend!
I'm trying to apply this macro to a spreadsheet that I have, but I'm having some issues. I keep getting a run time error. I've attached the file so that you can see what I'm working on.
Thanks for any help that you can lend!
Trosko, you are having the same issue that I faced, your deliminated column "L" has some blanks so the code errors out. I removed the rows with blanks in column L and the macro ran just fine.
So you'll need to update the macro to account for this. I wasn't able to figure that part out and in my case it didn't matter, rows with blanks in the deliminated column were not needed for my project so I was able to just delete those rows by inserting the following code in the macro. You'll notice I put the new code right after the delclarations and before the existing code.
Code: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 = "D" Const TableColumns As String = "A:D" Const StartRow As Long = 2 Columns(4).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Last edited by Excel Fox; 08-18-2013 at 08:41 PM. Reason: Code tag added
Hi Rick,
Excellent code, I must say, but I found a problem if, for any reason, a customer has the column C blank.
Something like (Joe has no Parts)
Name Client Parts Rick 1111111 P1, P2, P3 Sam 2222222 P2, P5 Joe 3333333 Bill 4444444 P4, P6, P7, P8
This line of code throws an error
Code:Intersect (Rows (X + 1), Columns (TableColumns)). Resize (UBound (Data)). XlShiftDown Insert
The solution I found is (modifications in blue)
Code:Sub RedistributeDataV2() Dim X As Long, LastRow As Long, A As Range, Table As Range, Data() As String Const Delimiter As String = ", " Const DelimitedColumn As String = "C" Const TableColumns As String = "A:C" 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) And UBound(Data) <> -1 Then Intersect(Rows(X + 1), Columns(TableColumns)).Resize(UBound(Data)).Insert xlShiftDown Cells(X, DelimitedColumn).Resize(UBound(Data) + 1) = WorksheetFunction.Transpose(Data) End If Next LastRow = Cells(Rows.Count, DelimitedColumn).End(xlUp).Row On Error GoTo NoBlanks Set Table = Intersect(Columns(TableColumns), Rows(StartRow) _ .Resize(LastRow - StartRow + 1, Columns(TableColumns).Columns.Count - 1)) On Error GoTo 0 For Each A In Table.SpecialCells(xlBlanks).Areas A.FormulaR1C1 = "=R[-1]C" A.Value = A.Value Next NoBlanks: Application.ScreenUpdating = True End Sub
This is reasonable or is there another more efficient solution?
Best Regards,
Marcelo
Thank you so much for pointing out this problem to me... I cannot believe it did not occur to me to test data where one or more of the cells are emptly.And thank you for your code suggestion as well; however, I decided to take a different route in fixing the problem. Your alert to me allowed me to review my code with "fresh eyes" and I saw a way to correct the problem while at the same time tighten up the code somewhat (I managed to eliminate the last For..Next loop entirely). I replaced the original code in Message #1 with my new code and put a note at the top indicating that message contains revised code (for those who previously visited this thread and then re-visit it in the future). Thank you again for pointing out the problem to me... I really appreciated it.
very nice. i was looking for this code. coincidentally, i searched before posting the question in Mrexcel forum and here i got exactly what i needed.
thank you Rick and Marcelo.
Last edited by Excel Fox; 08-18-2013 at 08:39 PM. Reason: Quote removed as it didn't add value to the post
Hello. This macro works beautifully, however is there a way to do this same function to multiple columns with multi-values?
My data looks like this:
Column 1 Column 2 Multi-value Column Multi-value column
Data Data RMA;-;RMA 65044;99944;65033
The multi-values all correspond with each other.
This is a break down of service work orders, line items such as labor, travel, parts and if it is returnable.
Your help is greatly appreciated.
Hi Rick,
Great piece of code
i have a macro data file that contains empty cells too. when i run this code it fills all the empty cells to the values to the upper cell whether needed or not.i want to copy only those rows which have delimited values in the (delimited) column.
Can you help me fix the code so that i only creates new rows for the splited column.
A B C
a b,c,d l
k u
v,k i
The results should be:
A B C
a b l
a c l
a d l
k u
v i
k k
Thanks
Wajiha![]()
I felt that I had to tell you that this is incredibly useful, and I thank you very much.
But to avoid falling foul of the rule "4. DO NOT post one liners thanking members or post remarks like "Wow, this is helpful", or "This is great" etc.", my situation has one additional requirement that you might be willing to look at.
In the table in post #1, the delimited "Parts Ordered" column has different data in each cell. Would it be possible to get the macro to skip a row entirely, if the "Parts Ordered" delimited data in row x is the same as in row (x-1)?
Hi Rick,
I have attached the file for your review. The file was too large to attach so I stripped most of the data leaving enough for you to work with. Also I changed the delimiter to ", " using the replace all function, was hoping that would help but it did not.
The other part that I was not sure of was where to place the macro, in the worksheet or module either way I get an error. The attachment currently has it in the module.
Thanks for taking a look:-)
Hi Rick,
I just figured out the issue, some rows do not have data in the Delimited column which makes the code error. Anyway to tweek the code so it bypasses such rows?
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://www.youtube.com/watch?v=2oT4qrHmDMY&lc=UgwviLabd7r_3KpP6wh4AaABAg. 9h5lFRmix1R9h78GftO_iE
https://www.youtube.com/watch?v=2oT4qrHmDMY&lc=UgyG714V_k7odQMrTz14AaABAg. 9h740K6COOA9h77HSGDH4A
https://www.youtube.com/watch?v=2oT4qrHmDMY&lc=UgyG714V_k7odQMrTz14AaABAg. 9h740K6COOA9h76fafzcEJ
https://www.youtube.com/watch?v=2oT4qrHmDMY&lc=UgyG714V_k7odQMrTz14AaABAg. 9h740K6COOA9h759YIjlaG
https://www.youtube.com/watch?v=2oT4qrHmDMY&lc=UgyG714V_k7odQMrTz14AaABAg. 9h740K6COOA9h74pjGcbEq
https://www.youtube.com/watch?v=2oT4qrHmDMY&lc=UgyG714V_k7odQMrTz14AaABAg
https://www.youtube.com/watch?v=2oT4qrHmDMY&lc=UgzJJUDVv2Mb6YGkPYh4AaABAg. 9h5uPRbWIZl9h7165DZdjg
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
Last edited by DocAElstein; 07-10-2023 at 07:32 PM.
Bookmarks