Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / June 2006

Tip: Looking for answers? Try searching our database.

copy and insert entire row based on integer in column A

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave A - 25 Jun 2006 09:14 GMT
Hello,

I have a spreadsheet that contains data as follows;

3  rgf rhh
2  xyz abc
4  ejerr
5  rrrr

I would like a macro that copies and inserts the entire row "x" times,
based on the value of the number in column A.

The file can be thousands of lines long.
Maximum number in column a is 50

desired  result

3  rgf rhh
3  rgf rhh
3  rgf rhh
2  xyz abc
2  xyz abc
4  ejerr
4  ejerr
4  ejerr
4  ejerr
5  rrrr
5  rrrr
5  rrrr
5  rrrr
5  rrrr

Thanks
Dave
Norman Jones - 25 Jun 2006 09:55 GMT
Hi Dave,

Try:

'=============>>
Public Sub Tester()
   Dim WB As Workbook
   Dim SH As Worksheet
   Dim rng As Range
   Dim i As Long, k As Long
   Dim LRow As Long

   Set WB = Workbooks("YourBook.xls")     '<<==== CHANGE
   Set SH = WB.Sheets("Sheet1")                  '<<==== CHANGE

   LRow = SH.Cells(Rows.Count, "A").End(xlUp).Row

   For i = LRow To 1 Step -1
       With SH.Cells(i, "A")
           k = .Value - 1
           If k > 0 Then
               .EntireRow.Resize(k).Insert
               .Offset(-1, 1).Resize(k).Value = _
                                       .Offset(0, 1).Value
               .Offset(-1).Resize(k).Value = .Value
           End If
       End With
   Next i

End Sub
'<<=============

---
Regards,
Norman

> Hello,
>
[quoted text clipped - 30 lines]
> Thanks
> Dave
Dave A - 26 Jun 2006 00:14 GMT
> Hi Dave,
>
[quoted text clipped - 66 lines]
> > Thanks
> > Dave

Thanks Norman.
Didn't work as I expected.
I suspect that the rows are being overwritten as this programme results
in black rows in between the expected data.
Norman Jones - 26 Jun 2006 01:38 GMT
Hi Dave,

> Didn't work as I expected.
> I suspect that the rows are being overwritten as this programme results
> in black rows in between the expected data.

Try this minor amendment:

'=============>>
Public Sub TesterX()
   Dim WB As Workbook
   Dim SH As Worksheet
   Dim rng As Range
   Dim i As Long, k As Long
   Dim LRow As Long

   Set WB = Workbooks("YourBook.xls")     '<<==== CHANGE
   Set SH = WB.Sheets("Sheet1")                   '<<==== CHANGE

   LRow = SH.Cells(Rows.Count, "A").End(xlUp).Row

   For i = LRow To 1 Step -1
       With SH.Cells(i, "A")
           k = .Value - 1
           If k > 0 Then
               .EntireRow.Resize(k).Insert
               .Offset(-k, 1).Resize(k).Value = _
               .Offset(0, 1).Value
               .Offset(-k).Resize(k).Value = .Value
           End If
       End With
   Next i

End Sub
'<<=============

---
Regards,
Norman
Dave A - 26 Jun 2006 01:56 GMT
Norman,

worked well.
There's only one minor problem.
The output has each row N+1 times based on the number in the first
column.
Since we have an "original" we only need to copy N-1 times.

Thanks again
Dave
Norman Jones - 26 Jun 2006 02:18 GMT
Hi Dave,

> There's only one minor problem.
> The output has each row N+1 times based on the number in the first
> column.

Your original post showed your raw data as:

     3 rgf rhh
     2 xyz abc
     4 ejerr
     5 rrrr

You showed the required resuks as:

     3 rgf rhh
     3 rgf rhh
     3 rgf rhh
     2 xyz abc
     2 xyz abc
     4 ejerr
     4 ejerr
     4 ejerr
     4 ejerr
     5 rrrr
     5 rrrr
     5 rrrr
     5 rrrr
     5 rrrr

Which is what my suggested code produces - indeed the above table is a
direct copy / paste of the data produced by the code.

> Since we have an "original" we only need to copy N-1 times.

This requirement is reflected in the lines:

           k = .Value - 1
           If k > 0 Then
               .EntireRow.Resize(k).Insert

k is the value in column A, k-1 rows are inserted.

If you are stll experiencing a problem, I can send you my test book in
response to an email:

norman_jones@NOSPAMbtconnectDOTcom

(Delete "NOSPAM" and replace "DOT" with a full stop [period] )

---
Regards,
Norman
Dave A - 26 Jun 2006 01:46 GMT
FYI
I found another script in this newsgroup which fundamently does what I
want.
exept it copies N+1 times instead of N. I can overide this with a crude
edit before I run the macro until I work out a better way.

Sub AddRows()
Dim i As Long, AdditionalRows As Long, NextRow As Long
Dim Cel As Range

For Each Cel In ActiveSheet.Range("A1").EntireColumn.Cells
   If Cel.Value = "" Then
       Exit For
   Else
       If IsNumeric(Cel.Value) Then
           AdditionalRows = AdditionalRows + CLng(Cel.Value) + 3
       End If
   End If
Next Cel

If ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row +
AdditionalRows < 65537 Then
   For Each Cel In ActiveSheet.Range("A1").EntireColumn.Cells
       If NextRow > 0 Then
           NextRow = NextRow - 1
           GoTo NextCel
       End If

       NextRow = 0
       If Cel.Value <> "" And IsNumeric(Cel.Value) Then
           For i = 1 To CLng(Cel.Value)
               Cel.Offset(1, 0).EntireRow.Insert
               Cel.EntireRow.Copy Cel.Offset(1, 0)
           Next i
           NextRow = NextRow + i - 1
       End If
NextCel:
   Next Cel
Else
   End
Norman Jones - 26 Jun 2006 01:59 GMT
Hi Dave,

> I found another script in this newsgroup which fundamently does what I
> want.
> exept it copies N+1 times instead of N. I can overide this with a crude
> edit before I run the macro until I work out a better way.

Did you try the revised code that I posted?

---
Regards,
Norman
Dave A - 26 Jun 2006 02:13 GMT
Did you try the revised code that I posted?

Yup. I missed your reply before I reposted.

Code works well, expect as commented above.
Each row copied once two many times as the orginal is retained.

Regards
Dave
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.