In the past week, I’ve been asked two similar questions about using VBA to copy multiple values from one sheet to another. More specifically, both people wanted to learn how to copy certain values in a particular order.
Since these are common questions, this is a great opportunity to teach you one way to do it. This Q&A session will show you how to copy every Nth cell in a range to a different sheet by using the Step keyword and a counter inside a VBA For Loop.
Q1: How do I pull data from every Nth cell of one sheet and copy it to consecutive cells in another sheet?
The questioner wanted to copy every 76th cell from one sheet to a second sheet, but he wanted the values placed in back-to-back cells on the second sheet.
The besy way to do this using a VBA macro is to use the Step keyword inside a For Loop. To make the values paste in consecutive cells, you would have a counter that increments by 1 inside the for loop.
Here’s an example of a macro that will solve the problem:
Sub CopyNthData() Dim i As Long, icount As Long Dim ilastrow As Long Dim wsFrom As Worksheet, wsTo As Worksheet Set wsFrom = Sheets("Sheet2") Set wsTo = Sheets("Sheet1") ilastrow = wsFrom.Range("B100000").End(xlUp).Row icount = 1 For i = 1 To ilastrow Step 76 wsTo.Range("B" & icount) = wsFrom.Range("B" & i) 'change formula to fit your needs icount = icount + 1 Next i End Sub
Becoming a VBA expert isn't hard
Over 5000 members are improving their VBA skills for free with our email tutorials. Why don't you join them? Our experts share time-saving VBA tips and we'll give you access to our huge macro library - it's sure to speed up your macro development.
This macro takes every 76th cell, starting with row 1, in column “B” of worksheet
icount variable controls what row the values get copied to. In the above example, it starts at row 1, then row 2, then row 3, and so on.
If you want to capture every 10th cell instead of every 76th, change the integer beside the
Step keyword in the
For loop. If you want to start copying values from row 2 instead of row 1, change the “1” in the
For i = 1... line
Change the column from column “B” if you’re interested in different columns and change the source and destination sheets by modifying the variables
Q2: I have values in Column "A" of Sheet1. How do I copy them to Sheet2 if I always want the last value in Sheet1 to appear in cell "A100" of Sheet2?
The user wanted the last value in Sheet1 to appear in cell “A100” of Sheet2, regardless of how many rows of data were entered on Sheet1.
Let’s say the person asking the question had data in cells A1 through A15 of Sheet1. The value in cell A15 of Sheet1 would go in cell A100 of Sheet2. Subsequently, the value in cell A14 of Sheet1 would go in cell A99 of Sheet2. Then, A13 of Sheet1 would be copied to A98 of Sheet2, and the pattern would continue until the last value in Sheet1 is copied over to Sheet2.
This a variation of Question 1, so the VBA answer will look quite similar.
Sub CopyData() Dim i As Long, icount As Long Dim ilastrow As Long Dim wsFrom As Worksheet, wsTo As Worksheet Set wsFrom = Sheets("Sheet1") Set wsTo = Sheets("Sheet2") ilastrow = wsFrom.Range("A100000").End(xlUp).Row icount = 100 If ilastrow > icount Then MsgBox "Too many entries to do what you want." Exit Sub End If For i = ilastrow To 1 Step -1 wsTo.Range("A" & icount) = wsFrom.Range("A" & i) icount = icount - 1 Next i End Sub
How does this work? First, the macro finds the last row in worksheet
Just like in Question 1, the variable
After some error checks, the macro starts at the last row and steps backward to the first cell. That’s the big difference between the two examples. Both the counter,
For loop step backward in the answer to this question.
Notice, the person asking the question was very specific about where he wanted the last value placed - cell
Find more great Question and Answer sessions over at the dedicated Excel VBA Tutorials page. I encourage you to check it out and share this article with a friend!
If you want to contribute more to this Q&A session or if you have questions of your own, let me know by leaving a comment!