PC Review


Reply
Thread Tools Rate Thread

Auto copy and insert a defined number of rows as defined in a cell

 
 
Billy
Guest
Posts: n/a
 
      17th Mar 2010
Hi
I don't know much about VBA but could probably work something out.
I need to produce carton labels from a spreadsheet using Word to mail merge
and the labels need to include 1 of 10, 2 of 10 on them. The number of
labels required is dependant on a number which is included for each row of
data in the spreadsheet. What I am doing at the moment is copying each row
and insert pasting the additional number of rows required. In the new rows I
then add 1 of 10 in the first row, 2 of 10 in the second row etc. etc. This
then enables me to perform the mailmerge.
I have around 300 label variants (300 different rows of data) which yield
around 2500 labels and I have to do this every 3 weeks. As you can imagine
it takes a crazy amount of time and requires a lot of checking.
I'd guess there must be a much easier way that even someone of my moderate
experience could execute but I need a few pointers to get me started.
Any advice greatly appreciated.
 
Reply With Quote
 
 
 
 
Project Mangler
Guest
Posts: n/a
 
      18th Mar 2010
Sub InsertRows()
'assumes column A contains the total number of rows required for each
'row of data
'assumes this cell can be overwritten with "1 of n"
'A3 taken as the first cell containing data
'assumes Sheet1 contains the data

'turn off screen updating for speed
Application.ScreenUpdating = False

'initialize variables
Dim rngEndcell As Range 'last occupied cell in column A
Dim rngInsrt As Range 'range of occupied cells in Column A
Dim L As Long 'number of rows to be inserted
Dim i As Long 'loop variable
Dim S As Long 'loop variable
Dim v As Long 'loop variable
Dim T As Range 'transient address of cell below which rows are
added

'get address of last occupied cell in column A
Set rngEndcell = Sheets("sheet1").Cells(Rows.Count, "A").End(xlUp)
'set the occupied range
Set rngInsrt = Range("A3", rngEndcell)
'initialise a loop variable with the number of rows
i = rngInsrt.Rows.Count

'step through the range from the bottom up
For S = i To 1 Step -1

'T holds the address of the cell below which rows are inserted
Set T = Range("A3").Offset(S - 1, 0)
'L holds the total number of rows required
L = Range("A3").Offset(S - 1, 0).Value

T = "1 of " & L
'perform the loop inserting rows and
'copying the initial row and setting the "1 of n" value
For v = 1 To L - 1
T.Offset(v, 0).EntireRow.Insert shift:=xlDown
T.EntireRow.Copy
T.Offset(v, 0).EntireRow.PasteSpecial xlPasteValuesAndNumberFormats
T.Offset(v, 0) = v + 1 & " of " & L
Next v

Next S

'get rid of crawling ants
Application.CutCopyMode = False

'select a finishing location for the cursor
Sheets("sheet1").Range("A1").Select

Application.ScreenUpdating = True

End Sub



"Billy" <(E-Mail Removed)> wrote in message
news:5408CEF5-CBA2-4C8D-83E3-(E-Mail Removed)...
> Hi
> I don't know much about VBA but could probably work something out.
>I need a few pointers to get me started.
> Any advice greatly appreciated.



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding names to a cell - application-defined or object-defined error Chris Microsoft Excel Discussion 1 20th Sep 2007 08:31 PM
Insert a cell into a defined place (or bookmark) in a word doc =?Utf-8?B?cmFwaGllbDIwNjM=?= Microsoft Excel Programming 1 7th Sep 2007 01:45 PM
Insert Row by a defined Number comotoman Microsoft Excel Misc 4 11th Oct 2005 09:50 PM
auto deleting defined rows =?Utf-8?B?TG9yaU0=?= Microsoft Excel Misc 4 4th Aug 2005 06:20 PM
Application-defined or object-defined error on copy Josh Sale Microsoft Excel Programming 3 3rd Feb 2005 05:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:07 PM.