Copy Paste to Every Nth Row

alexb523

Board Regular
Joined
Dec 12, 2013
Messages
115
Hello,

I am having issues copying the same thing 'n' amount of times to every 'n' row. i have a range of cells and would like to post it every over and over again (i have a code to get the respective values). I do this several different times and paste it a different amount of times. Here is the 'hard code' of what i need done:

Code:
    'Count of Mode
    Dim modeTable As Worksheet
    Set modeTable = Sheets.Add(After:=Sheets(Worksheets.Count))
    modeTable.Name = "modeTable"
    
    TableTemplates.Range("A67:G84").Copy
    
    modeTable.Range("A2").Select
    ActiveSheet.Paste
    modeTable.Range("A21").Select
    ActiveSheet.Paste
    modeTable.Range("A40").Select
    ActiveSheet.Paste
    modeTable.Range("A59").Select
    ActiveSheet.Paste
    modeTable.Range("A78").Select
    ActiveSheet.Paste
    modeTable.Range("A97").Select
    ActiveSheet.Paste
    modeTable.Range("A116").Select
    ActiveSheet.Paste
    modeTable.Range("A135").Select
    ActiveSheet.Paste
    modeTable.Range("A154").Select
    ActiveSheet.Paste
    modeTable.Range("A173").Select
    ActiveSheet.Paste
    modeTable.Range("A192").Select
    ActiveSheet.Paste
    modeTable.Range("A211").Select
    ActiveSheet.Paste
    modeTable.Range("A230").Select
    ActiveSheet.Paste
    modeTable.Range("A249").Select
    ActiveSheet.Paste


and then here is my attempt loop it. but i keep getting errors every which way.



Code:
    'count number of 'Count of Mode' table needed
    TableNames.Select
    Dim modeCount As Integer
    Dim tableRowDiff As Integer
    modeCount = Application.WorksheetFunction.CountIf(Range("A2:A" & lrTableNames), "Count of Mode") 'how many times i need to post a new table resides on a different spreadsheet
    tableRowDiff = 19
    
    For modeRow = 1 To modeCount
        modeTable.Range("A" & (modeRow * tableRowDiff)).Select
        ActiveSheet.Paste
    Next modeRow
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I'm not really sure where the 249 comes from. I like the idea of putting in a step. The error i'm currently getting is on
Code:
 [COLOR=#333333]modeTable.Range("A" & (modeRow * tableRowDiff)).Select[/COLOR]
where is throwing the error 'Select method of Range class failed'd
 
Upvote 0
o right. sorry. i'm trying to avoid knowing that number. how would i use the modeCount? in this case there are 14. but it varies over what i need my code for.
 
Upvote 0
So just to clarify what i am trying to do:

I have a range of cells
Code:
TableTemplates.Range("A67:G85").Copy
that i want to paste as many times as i "Count of Mode" appears in a specified sheet:

Code:
    TableTemplates.Range("A67:G85").Copy
    
    'count number of 'Count of Mode' table needed
    TableNames.Select
    Dim modeCount As Integer
    Dim tableRowDiff As Integer
    modeCount = Application.WorksheetFunction.CountIf(Range("A2:A" & lrTableNames), "Count of Mode")
    tableRowDiff = 19
    
    For modeRow = 2 To modeCount * 19 Step 19
        modeTable.Range("A" & modeRow).Select
        ActiveSheet.Paste
    Next modeRow
 
Upvote 0
So lets say there is a table (not an actual table just a matrix) like the one below from A1:C6 on a tab called Tab1:

Code:
[TABLE="width: 192"]
<tbody>[TR]
[TD="colspan: 3"]Table With Formatting[/TD]
[/TR]
[TR]
[TD]Header1[/TD]
[TD]Header2[/TD]
[TD]Header3[/TD]
[/TR]
[TR]
[TD]x[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD]y[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD]z[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]7[/TD]
[/TR]
[TR]
[TD]f[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]9[/TD]
[/TR]
</tbody>[/TABLE]

*NoteL the line 'Table With Formatting' is apart of the table

Then there is another tab, Tab2, with data in column A like the following:

Code:
[TABLE="width: 107"]
<tbody>[TR]
[TD]Column to Count[/TD]
[/TR]
[TR]
[TD]Other Text[/TD]
[/TR]
[TR]
[TD]Other Text[/TD]
[/TR]
[TR]
[TD]Other Text[/TD]
[/TR]
[TR]
[TD]Test Text[/TD]
[/TR]
[TR]
[TD]Test Text[/TD]
[/TR]
[TR]
[TD]Test Text[/TD]
[/TR]
[TR]
[TD]Count of Mode[/TD]
[/TR]
[TR]
[TD]Count of Mode[/TD]
[/TR]
[TR]
[TD]Count of Mode[/TD]
[/TR]
[TR]
[TD]Count of Mode[/TD]
[/TR]
[TR]
[TD]Count of Mode[/TD]
[/TR]
</tbody>[/TABLE]

There ends of being 5 instances of 'Count of Mode' (the number of instance will be unknown) which means i need to paste table, in this case, 5 times to a new sheet say called Tab3. The table would be pasted to A2, A9, A15, etc. which leave one row in between each table. This would be a straight copy-paste, formats and all.

The part which bugs in my previous code is the line:
Code:
[COLOR=#333333]modeTable.Range("A" & modeRow).Select[/COLOR]
which give the error 'Select method of Range class failed'

Thanks,
Alex
 
Last edited:
Upvote 0
This?


Excel 2010
ABC
1Table With Formatting
2Header1Header2Header3
3x15
4y26
5z37
6f49
Tab1



Excel 2010
A
1Column to Count
2Other Text
3Other Text
4Other Text
5Test Text
6Test Text
7Test Text
8Count of Mode
9Count of Mode
10Count of Mode
11Count of Mode
12Count of Mode
Tab2


Code:
Sub pastetopage()
Dim i%, lr%, x%
lr = Sheets("Tab2").Cells(Rows.Count, 1).End(xlUp).Row
x = Application.CountIf(Sheets("Tab2").Cells(1, 1).Resize(lr), "Count of Mode")
For i = 2 To 2 + (x * 6) Step 7
Sheets("Tab1").Cells(1).CurrentRegion.Copy Destination:=Sheets("Tab3").Cells(i, 1)
Next
End Sub


Excel 2010
ABC
1
2Table With Formatting
3Header1Header2Header3
4x15
5y26
6z37
7f49
8
9Table With Formatting
10Header1Header2Header3
11x15
12y26
13z37
14f49
15
16Table With Formatting
17Header1Header2Header3
18x15
19y26
20z37
21f49
22
23Table With Formatting
24Header1Header2Header3
25x15
26y26
27z37
28f49
29
30Table With Formatting
31Header1Header2Header3
32x15
33y26
34z37
35f49
Tab3
 
Last edited:
Upvote 0
I'm still having an issue when i put this in practical purpose. It seem to only be copying once to A14 to my modeTable tab.

Code:
    Dim modeTable As Worksheet
    Set modeTable = Sheets.Add(After:=Sheets(Worksheets.Count))
    modeTable.Name = "modeTable"
    


    'count number of 'Count of Mode' table needed
    TableNames.Select
    Dim modeCount As Integer
    modeCount = Application.WorksheetFunction.CountIf(Range("A2:A" & lrTableNames), "Count of Mode")
    
    For modeRow = 2 To 2 + (modeCount * 18) Step 19
        TableTemplates.Range("A67:G84").Copy Destination:=modeTable.Cells(modeCount, 1)
    Next
 
Upvote 0

Forum statistics

Threads
1,215,194
Messages
6,123,569
Members
449,108
Latest member
rache47

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