Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Excel copay and pasted specifed number of times macro

Posted on 2013-12-02
2
Medium Priority
?
451 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 2000 total points
ID: 39690485
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
ID: 39690739
Works Great!  Thanks!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

783 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