Solved

MS Excel copay and pasted specifed number of times macro

Posted on 2013-12-02
2
444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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 code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

623 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