Spliiting and tranposing a cell seperated by commas and copying all other info in other collumns

Vick3479

New Member
Joined
Mar 20, 2013
Messages
10
This macro works for one work book but wont for another and i dont know why they are the same but have diffrent values in the cell...i keep getting debug on this part of the macro in a diffrent work book "Range("D</SPAN>" & i) = MyArr(0)" please help</SPAN>




Sub VictorMartinez()</SPAN>
Dim MyArr, v As Long, i As Long, LR As Long</SPAN>
Application.ScreenUpdating = False</SPAN>
LR = Range("A" & Rows.Count).End(xlUp).Row</SPAN>

For i = LR To 2 Step -1</SPAN>
MyArr = Split(Range("D</SPAN>" & i), ", ")</SPAN>
Range("D</SPAN>" & i) = MyArr(0)</SPAN>
For v = 1 To UBound(MyArr)</SPAN>
Rows(i + v).Insert xlShiftDown</SPAN>
Range("A" & i + v, "BO</SPAN>" & i + v).Value = Range("A" & i, "BO</SPAN>" & i).Value</SPAN>
Range("D</SPAN>" & i + v) = MyArr(v)</SPAN>
Next v</SPAN>
Next i</SPAN>

Application.ScreenUpdating = True</SPAN>
End Sub</SPAN>
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Easiest thing I can see here that would break is if a cell in column D did not contain ", ". If that were the case, you would crash at For v = 1 To UBound(MyArr). You may want to add an if / then for the ubound(myArr)... If ubound(MyArr)>0 then... for the remainder of the loop.
 
Upvote 0
Another thought... I do not think you can split null or a blank cell. That would probably throw an error. You might want to test for that before you split as well...
 
Upvote 0
If it does not have a comma it just leaves it alone, and in my instance the cell its transposing will always have somthing in it, as for this ''You may want to add an if / then for the ubound(myArr)... If ubound(MyArr)>0 then... for the remainder of the loop.'' im not understanding what to do to the macro?
 
Upvote 0
I mean something like this...


Code:
Sub VictorMartinez()
Dim MyArr, v As Long, i As Long, LR As Long
Application.ScreenUpdating = False
LR = Range("A" & Rows.Count).End(xlUp).Row

For i = LR To 2 Step -1
    If Range("D" & i) <> "" Then
        MyArr = Split(Range("D" & i), ", ")
        Range("D" & i) = MyArr(0)
        If UBound(MyArr) > 0 Then
            For v = 1 To UBound(MyArr)
                Rows(i + v).Insert xlShiftDown
                Range("A" & i + v, "BO" & i + v).Value = Range("A" & i, "BO" & i).Value
                Range("D" & i + v) = MyArr(v)
            Next v
        End If
    End If
Next i

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Or if u no another macro i could use...Basically i want Cell D2-D_ to be seperated by commas and transposed, and all the other info in that row copied down into the inserted rows.
 
Upvote 0
Just thought I would have some fun with this problem... here is a non-looping macro that should do the same thing that CodeNinja's macro does.
Code:
Sub ForVictorMartinez()
  Dim LR As Long, Commas As Long
  LR = Cells(Rows.Count, "A").End(xlUp).Row
  Commas = Evaluate("SUM(IF(LEN(D2:D" & LR & "),1+LEN(D2:D" & LR & _
           ")-LEN(SUBSTITUTE(D2:D" & LR & ","","","""")),0))") + 1
  With WorksheetFunction
    Range("A2:A" & Commas) = .Transpose(Split(Join(.Transpose(Evaluate("IF(LEN(A2:A" & LR & _
                             "),REPT(A2:A" & LR & "&CHAR(1),1+LEN(D2:D" & LR & _
                             ")-LEN(SUBSTITUTE(D2:D" & LR & ","","",""""))),"""")")), ""), Chr(1)))
    Range("D2:D" & Commas) = .Transpose(Split(Join(.Transpose(Range("D2:D" & LR)), ", "), ", "))
  End With
End Sub
 
Last edited:
Upvote 0
Change the line


MyArr = Split(Range("D" & i), ", ")

with
MyArr = Split(Range("D" & i), ". ")


</pre>
 
Upvote 0

Forum statistics

Threads
1,203,145
Messages
6,053,753
Members
444,681
Latest member
Nadzri Hassan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top