Macro to copy/paste down X times (where X is the number of rows)

Solved/Closed
JW32 Posts 8 Registration date Friday January 4, 2013 Status Member Last seen February 21, 2013 - Feb 20, 2013 at 11:12 PM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Jul 8, 2019 at 11:19 AM
Hello,

I could sure use some help on a Macro.

I need to copy the value in E1 and paste down to all cells in column E (E2 to E??)

The issue is - the number of rows will change.

One time it might need to copy down 5 times. Another it might need to copy down 40 times.

I would also need it to do nothing if there is only 1 row (Nothing to copy if the source is the only row!!)

I case it matter you - I would repeat the exact process for Column F. Copy F1 and paste down to all cells in column F (F2 to F??).

We can NOT copy the row as the information in A to D must not be manipulated.

Thank you very much for your time!
Jeanine


10 responses

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 555
Updated by TrowaD on 14/03/17 at 12:07 PM
Hi Jusip16,

As per your sample data, the following code will do as requested.
The result will be placed in a second sheet. First sheet is called Sheet1 and the second sheet is called Sheet2. Either name your sheets like that or find those sheet references in the code and change them to match your (easily done by selecting entire code [CTRL+a] and use the find/replace window [CTRL+h]).

Here is the code:
Sub RunMe()
Dim x, lRow As Integer

lRow = Range("A" & Rows.Count).End(xlUp).Row
x = 1
Sheets("Sheet1").Select
With Sheets("Sheet2")
    Do
        x = x + 1
        If Cells(x, "C").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "C").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "C").Value
        End If
        If Cells(x, "D").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "D").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "D").Value
        End If
        If Cells(x, "E").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "E").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "E").Value
        End If
        If Cells(x, "F").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "F").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "F").Value
        End If
        If Cells(x, "G").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "G").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "G").Value
        End If
        If Cells(x, "H").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "H").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "H").Value
        End If
        If Cells(x, "I").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "I").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "I").Value
        End If
        If Cells(x, "J").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "J").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "J").Value
        End If
        If Cells(x, "K").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "K").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "K").Value
        End If
        If Cells(x, "L").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "L").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "L").Value
        End If
        If Cells(x, "M").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "M").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "M").Value
        End If
        If Cells(x, "N").Value <> vbNullString Then
            .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "A")
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Sheets("Sheet1").Cells(x, "B")
            .Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(1, "N").Value
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Cells(x, "N").Value
        End If
        
    Loop Until x = lRow
End With
End Sub


Best regards,
Trowa
Monday, Tuesday and Thursday are usually the days I'll respond. Bear this in mind when awaiting a reply.
11
hi Trowa,

I am searching for a code that will copy a number 80 times down a column, then move on to the next number and copy that number down 80x.

My inputs are a list (it will change every week) of numbers:
1
3
4
78
98
108

I wish to put these numbers in a column beside it,but repeating the numbers 80 times.

Output:
1
1
1
1
(76 more 1's down)
3
3
3
3
(76 more 3's down)

thank you for any help!
0
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 555 > dm
Mar 21, 2017 at 12:15 PM
Hi dm,

Assuming your data is located in column A, then the following code will place the result in column B:
Sub RunMe()
Dim x, y As Integer
x = 1
For Each cell In Range("A1", Range("A1").End(xlDown))
    For y = 1 To 80
        Range("B" & x).Value = cell.Value
        x = x + 1
    Next y
Next cell
End Sub


Best regards,
Trowa
0