View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.misc
Toppers Toppers is offline
external usenet poster
 
Posts: 4,339
Default Split cell data in mutiple rows

TRY ...

Sub CostC()

Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
Dim lasrow As Long, r As Long, rr As Long

rr = 1
ws1.Cells(1, 1).Resize(1, 4).Copy ws2.Cells(rr, 1)
With ws1
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
ns = 1
Do
rr = rr + 1
ws1.Cells(r, 1).Resize(1, 4).Copy ws2.Cells(rr, 1)
n = InStr(ns, .Cells(r, 3), ",")
If n < 0 Then
ws2.Cells(rr, 3) = Mid(.Cells(r, 3), ns, n - ns)
ns = n + 1
Else
ws2.Cells(rr, 3) = Mid(.Cells(r, 3), ns, 255)
End If
Loop Until n = 0
Next r
End With
End Sub

HTH

" wrote:

Hello all,
I have a excel worksheet as :

Name Address Cost center Class
A 123 345,234 Q
B 122 34 Q
C 1 3222,34,56 Q

But now i want to write a macro so that i can separate the comma values
under the cost center column into different rows,
So my output looks like


Name Address Cost center Class
A 123 345 Q
A 123 234 Q
B 122 34 Q
C 1 3222 Q
C 1 34 Q
C 1 56 Q

Can this be done with the help of macro and once i get the data in this
format , i need to run some other macros too, so i want it into the
same worksheet if possible

Thanks