Solved

MS Excel copay and pasted specifed number of times macro

Posted on 2013-12-02
2
427 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

809 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