Solved

MS Excel copay and pasted specifed number of times macro

Posted on 2013-12-02
2
400 Views
Last Modified: 2013-12-02
I need a macro that will look at a range of cells starting in A1 (the number of columns and number of rows will vary each time) and the last column will contain a number.  I need the macro to copy the preceeding columns in each row and paste it in the next worksheet that number of times

Example:
zone1   area    region   5
zone3   area    region   4
zone9   area    region   2
zone8   area    region   1

Will result in:
zone1   area    region
zone1   area    region
zone1   area    region
zone1   area    region
zone1   area    region
zone3   area    region
zone3   area    region
zone3   area    region
zone3   area    region
zone9   area    region
zone9   area    region
zone8   area    region

Again the number of rows and columns will vary but the last column will always include the number of times to copy and paste and the region will start in A1
0
Comment
Question by:djfab99
2 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
Comment Utility
This will clear the contents of the target sheet and copy the new data.
Sub ReptPerLastCol()
    Dim sws As Worksheet
    Dim tws As Worksheet
    Dim sr As Range
    Dim rw As Range
    Set sws = Sheets("Sheet1")
    Set tws = Sheets("Sheet2")
    Set sr = sws.Range("A1").CurrentRegion
    tws.Cells.ClearContents
    For Each rw In sr.Rows
        rw.Resize(, sr.Columns.Count - 1).Copy _
            Sheet2.Range("A" & Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1).Row).Resize(rw.Cells(1, sr.Columns.Count))
    Next rw
End Sub

Open in new window

0
 

Author Closing Comment

by:djfab99
Comment Utility
Works Great!  Thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now