Copy Paste to another sheet

erenkey

Board Regular
Joined
Dec 9, 2005
Messages
162
I have a spreadsheet that has First Name, Last Name, and a number in Columns ABC. Is there a way to automatically copy and paste each name into another sheet the same number of times as the number shown?

Example:

Sheet1
A........................B............................C
First Name............Last Name................Number
John Smith 5
Tom Jones 3


Sheet2

A........................B
First Name............Last Name
John Smith
John Smith
John Smith
John Smith
John Smith
Tom Jones
Tom Jones
Tom Jones


Is this possible?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,

see if this is ok.
I have used sheet2 as the list of names change to suit throughout code and sheet 3 as the target

Code:
Sub Repeat_Names()
Worksheets("Sheet2").Select
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
 
    For Each cell In Rng
    num = cell.Row
    x = cell.Offset(0, 2).Value
    For i = 1 To x
 
    Worksheets("Sheet2").Range("A" & num & ":B" & num).Copy
 
    Worksheets("Sheet3").Select
    Range("A1").Activate
 
    Do While Not IsEmpty(ActiveCell)
    ActiveCell.Offset(1, 0).Select
    Loop
Selection.PasteSpecial
Next
Next
End Sub
 
Upvote 0
Hi,

Try it on a copy of the workbook.

Open the workbook that contains your names list.
Open the VBA editor (Alt F11)
On the left window at the top should be VBA project - right-click and select insert > module
Paste the code in the module.

Then select run on the toolbar.

Post any errors and the line that caused it.
What is the name of the sheet you have the list of names on?
The code below has been changed to expect sheet1 and will split the names to sheet2.

Code:
Sub Repeat_Names()
rn = 1
Worksheets("Sheet1").Select
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
 
    For Each cell In Rng
    num = cell.Row
 
    x = cell.Offset(0, 2).Value
 
    For i = 1 To x
 
    Worksheets("Sheet1").Range("A" & num & ":B" & num).Copy Destination:=Worksheets("Sheet2").Range("A" & rn)
    rn = rn + 1
 
Next
Next
End Sub
 
Last edited:
Upvote 0
I ran it and got a run-time error '13' Type mismatch. I clicked on the debug button and it highlighted

For i = 1 To x
 
Upvote 0
Just another way. Please note that I happened to use the default codenames of sheet3 and sheet4.
Rich (BB code):
Option Explicit
    
Sub exa4()
Dim rng As Range, Cell As Range
    
    With Sheet3
        Set rng = Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
        
        For Each Cell In rng
            Sheet4.Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(Cell.Offset(, 2).Value, 2) = Cell.Resize(, 2).Value
        Next
    End With
End Sub
 
Upvote 0
Hi ,

does column C only contain numbers.

Step through the code in the VBA editor by using F8 until you get to

For i = 1 To x

hover over x on the previous line and it should show its value?
 
Upvote 0
Sorry,

didn't take the headers into account.

The range should start from A2.
Change the red bit.

Code:
Set Rng = Range(Range("[COLOR=red][B]A2[/B][/COLOR]"), Range("A" & Rows.Count).End(xlUp))
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top