Text to n rows + copy row and insert n times

Solved/Closed
Duffy - Apr 7, 2012 at 09:31 AM
 Duffy - Apr 30, 2012 at 01:14 AM
Hello,

I would appreciate your help with a macro I am trying to create in Excel 2007. My data is as follows:

Column1 Column2 Column3 Column4
Data1 Data1 Name1;Name2;Name3 Data1
Data2 Data2 Name1;Name2;Data2
Data3 Data3 Name1;Name2;Name3 Data3

Each cell in column 3 contains n number of names separated with a semicolon.

I need a macro that does these things:

1) Create n number of rows after the first row. N is the number of names in the cell in the first of column 3.

2) Separate the names in the rows below. (Similar to Text to Columns)

3) Copy the content of the other cells in the original row to the inserted rows below.

4) Proceed to the next row and do it all again.

The outcome should look like this:

Column1 Column2 Column3 Column4
Data1 Data1 Name1 Data1
Data1 Data1 Name2 Data1
Data1 Data1 Name3 Data1

Data2 Data2 Name1 Data2
Data2 Data2 Name2 Data2

Data3 Data3 Name1 Data3
Data3 Data3 Name2 Data3
Data3 Data3 Name3 Data3

Can you help me out?


Related:

34 responses

venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 21, 2012 at 11:10 PM
are we looking at different duffy 5 files.

am sending the file now called "duffy5 EXTRACT SHT 1 AND 2W.xlsm"
download this file from

http://speedy.sh/JJcvY/duffy5-EXTRACT-SHT-1-AND-2W.xlsm

see sheet2

for retesting I ran the macro " testtwo" again and it came out alright

u can also retest by running testtwo
give me feedback
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 22, 2012 at 03:48 AM
your original files e.g. duffy5 contains in column L in each row number of strings (0,1,or more than 1) separated by a semicolon
so you wanted the other columns (col. other than L should be repeated for each string of column L of a particular row. and that why the macro was written

but in your latest "feeback file" there is ONLY ONE string in each row in sheet1. then why do you need the macro
because sheet 2 is same as sheet1

I agree even then the macro should work. I found when I keep sheet2 as active sheet, the macro works ok. but if I keep sheet1 as active sheet the problem as u said arises. I found the small (but serious) bug (one dot is missing in the code statement
Cells(dest.Offset(n - 1, 0).Row, "L") = nname(n)
it should have a dot in the beginning
.Cells(dest.Offset(n - 1, 0).Row, "L") = nname(n)

BY THE BY YOU NEED NOT RUN BOTH THE MACROS(though no catastrophic error will occur). IT IS ENOUGH IF YOU RUN "TESTTWO". you can see the second macro undo is incorporated in the Testtwo macro somewhere in the beginning. Of course both the macros should be parked in the module

you have to test with various choices of combinations in column L of sheet 1 and test the macro for possible bug.;

I am uploading the file now called
"duffy5 EXTRACT SHT 1 AND 2W_Feedback 120422.xlsm"
download this file from
http://speedy.sh/ss8TC/duffy5-EXTRACT-SHT-1-AND-2W-Feedback-120422.xlsm

YOU DISCARD ALL OLD FILES TO AVOID CONFUSION.AND KEEP THIS AS CORRECT FILE. even if you find some future bug let us change the file name later.
0
Hi,

Now it seems to work perfectly! I will continue with this and see if I discover any bugs.

Thank you so much for your help! I am eternally grateful!

Best,

Duffy
0
Hi,

Is it possible to modify the code so, that you can have up to 100 columns that it copies? The column L has the names separated by semicolon.

Here is an example:

http://speedy.sh/qqN5e/duffy5-EXTRACT-SHT-1-AND-2W-Feedback.xlsm

Best,

Duffy
0

Didn't find the answer you are looking for?

Ask a question
Hi,

I found one bug:

If there is one blank cell to the right of the column L, for example in the cell N5, it leaves the whole row 5 empty from column N onwards in sheet 2.

So if there is an empty cell in N5, then in sheet2 there are empty cells also in cells N5:AC5...

Do you have the same issue?

Duffy
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 22, 2012 at 11:26 PM
quote
Is it possible to modify the code so, that you can have up to 100 columns that it copies? The column L has the names separated by semicolon. unquote

got confuse again

THE MACRO IS THE OLD MACRO.AND NOT THE NEW MACRO i HAVE SENT ON SUNDAY.
TO AVOID THIS TYPE OF CONFUSION EITHER DELETE OR SEND AWAY THE OLD FILES SOME WHERE
anyhow I copied the correct macro and yet modified small changes
to avoid confusion I called this macro as TESTTHREE120423



I am sending you only the macro undo and testshree120423
Open a new file and put some data in sheet 1 and copy this macro in that file and save the file with a new name in your hard disk

Now test various options with the new macro and take this as the latest.

Sub undo()
With Worksheets("sheet2")
.Cells.Clear
End With
End Sub



Sub testthree120423()
Dim m As Long, j As Long, k As Long, semicolon() As Long
Dim n As Long, nname() As String
Dim r As Range, start As Long
Dim dest As Range
Dim r1 As Range
Dim length As Long
Application.ScreenUpdating = False
start = 1
undo
With Worksheets("sheet1")
j = .Range("a2").End(xlDown).Row
For k = 3 To j
Set r = .Cells(k, "L")
'MsgBox r.Address
With r
m = Len(r.Value) - Len(WorksheetFunction.Substitute(r, ";", ""))
'MsgBox m
ReDim nname(1 To m + 1)

If m = 0 Then
nname(1) = r.Value
GoTo m_is_zero
End If
End With
ReDim semicolon(1 To m)


'ReDim nname(1 To m + 1)
For n = 1 To m + 1
'msgbox n
If n = m + 1 Then
nname(n) = Mid(r, start, Len(r) - start + 1)
'msgbox nname(n)
GoTo nextstep
End If
'msgbox start
semicolon(n) = InStr(start, r, ";")
'msgbox semicolon(n)
length = semicolon(n) - start
'msgbox length
nname(n) = Mid(r, start, length)
'msgbox nname(n)
start = semicolon(n) + 1
'msgbox start
Next n
nextstep:
m_is_zero:
.Range(.Cells(k, 1), .Cells(k, "K")).Copy
With Worksheets("sheet2")
Set dest = .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
Range(dest, dest.Offset(m, 0)).PasteSpecial
For n = 1 To m + 1
'dest.Offset(n - 1, 0).End(xlToRight).Offset(0, 1) = nname(n)
.Cells(dest.Offset(n - 1, 0).Row, "L") = nname(n)
Next n
End With
'Set r1 = Range(.Cells(k, "M"), .Cells(k, "M").End(xlToRight))

Set r1 = Range(.Cells(k, "M"), .Cells(k, Columns.Count).End(xlToLeft))
r1.Copy
With Worksheets("sheet2")
Set dest = .Cells(Rows.Count, "M").End(xlUp).Offset(1, 0)
Range(dest, dest.Offset(m, 0)).PasteSpecial
End With
start = 1
Next k
End With
With Worksheets("sheet2")
'Range(.Range("a2"), .Range("A2").End(xlDown)).EntireRow.AutoFit
'Range(.Range("A2"), .Range("a2").End(xlToRight)).EntireColumn.AutoFit

Range(.Range("a2"), .Cells(Rows.Count, "A").End(xlUp)).EntireRow.AutoFit
Range(.Range("A2"), .Cells(2, Columns.Count).End(xlToLeft)).EntireColumn.AutoFit

End With
Application.ScreenUpdating = True
Application.CutCopyMode = False
MsgBox "macro over"
End Sub
0
Hi,

Thanks for the fast reply.

I found one major bug:

http://speedy.sh/mmexk/testshree120423-BUG.xlsm

Please open the file and check this:

1) In sheet 1 pay attention to the yellow row.

2) I ran the macro, and in sheet 2 the yellow row is in the correct place in the beginning. However, the macro aligns the rows wrong after Column L.

It is hard to explain, see it yourself.

Best,

Duffy
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 23, 2012 at 10:28 PM
I shall look into that.
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 24, 2012 at 12:00 AM
I would like you to have a look at the golden rules given by an top expert in excel and excel vba

https://www.ozgrid.com/forum/index.php?thread/76234-excel-statistical-functions-trend/
in those rules see item 5

If you are going ask somebody else(who is naturally busy with other works) to write an omnibus macro for all occasions it would be preferable to avoid BLANK CELLS.
in the present case
in sheet 1 M3 is blank and copied to sheet 2 the cells in M are blanks in the first two rows.
and there the problem occurs




I understand your main database may not be yours but designed by somebody else. In that case it would be preferble to fill up the blank cells with somethng for eg. "x". for this you can have small macro

when you present tg somebody else at the VERY LAST STAGE YOU CAN CLEAR THESE Xs IF YOU DESIRE SO

are you amenable to this

reply immediately
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 24, 2012 at 12:17 AM
forf your perusal I am sending the file "DUFFY expt 120424.xlsm"

download this from

http://speedy.sh/tt8Qv/DUFFY-expt-120424.xlsm


now run ONLY testthree120424

and see how it is in sheet2

if you are not amenable to x let me know.
0
Hello,

X is fine with me. Now it works perfectly. Thank you so much!

Best,

Duffy
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 24, 2012 at 10:40 PM
you are welcome. but keep experimenting. some bug may come up. in that case do not hesitate to post back. It was an interesting project. best compliments.
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Apr 25, 2012 at 10:24 PM
was three any problem with the macro,. got a message that L col not filled. I do not know this is an old message redirected or a new one.
0
Nope, no problems at all. Everything works great and my findings seem to be statistically significant :)

Duffy
0