VBA Copy a column to nth colums in Excel

Coenieh

New Member
Joined
Oct 18, 2018
Messages
19
Hi All,

I'm new to this blog, so I hope I follow the rules correctly. Thanks for accepting me.

I need help setting up a macro/VBA to copy a specific column (G) range "G1:G550", which contains data in some cells and formulas in the others others. The column needs to be copied to the next 50 columns to every 2nd column, starting from Column I then K then M etc.

I've tried the following code but it does not seem to work:

Code:
Sub ColCopy()


Dim cpval As Range
Dim lastRow As Long


With Worksheets("Sheet4")
    lastRow = .Cells(Rows.Count, "G").End(xlUp).Row
    Set cpval = .Range("G550:G" & lastRow)


    For colx = 9 To 50 Step 2
        .Range(.Cells(550, colx), .Cells(lastRow, colx)).Value = cpval.Value
    Next
End With
End Sub

I would appreciate any help. Please let me know should you require more information.

Thank You
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I need help setting up a macro/VBA to copy a specific column (G) range "G1:G550",
Because it is a fixed range 'Range("G1:G550")' then you don't need to find last row.

Code:
Sub ColCopy()

Dim cpval As Range
With Worksheets("Sheet4")
    Set cpval = .Range("G1:G550")

    For colx = 9 To 50 Step 2
        .Range(.Cells(1, colx), .Cells(550, colx)).Value = cpval.Value
    Next
End With
End Sub
 
Upvote 0
Hi AlphaFrog,

It is not copying or pasting anything. When I change some of the parameters it will only copy the text and not the formulas but then does not paste to the intended colums.
 
Upvote 0
Hi Akuini,

I have tried various options with the range as "G550:G" and "G1:G550"
Code:
.Range("G550:G" & lastRow)

But it does not work. Any idea of how I can copy column G and paste it into every 2nd column after e.g. I, K, M etc.
 
Upvote 0
Hi Akuini,

I have tried various options with the range as "G550:G" and "G1:G550"
Code:
.Range("G550:G" & lastRow)

But it does not work. Any idea of how I can copy column G and paste it into every 2nd column after e.g. I, K, M etc.

You mean it doesn't copy paste anything at all?
Actually the code I gave you works for me, I've tried it. Don't know why it don't work for you.
The data is in "sheet4", isn't it?
 
Upvote 0
I just realized that you want to copy "all" not just value.
In this case use this code below:
But again, the previous code should copy paste something i.e the value.

Code:
[COLOR=blue]Sub[/COLOR] ColCopy1[B]()[/B]
[COLOR=blue]Dim[/COLOR] colx [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]
[COLOR=blue]With[/COLOR] Worksheets[B]([/B][COLOR=brown]"Sheet4"[/COLOR][B])[/B]
    [COLOR=blue]For[/COLOR] colx [B]=[/B] [B][COLOR=crimson]9[/COLOR][/B] [COLOR=blue]To[/COLOR] [B][COLOR=crimson]50[/COLOR][/B] [COLOR=blue]Step[/COLOR] [B][COLOR=crimson]2[/COLOR][/B]
        [B].[/B]Range[B]([/B][COLOR=brown]"G1:G550"[/COLOR][B]).[/B]Copy [B].[/B]Range[B](.[/B]Cells[B]([/B][B][COLOR=crimson]1[/COLOR][/B][B],[/B] colx[B]),[/B] [B].[/B]Cells[B]([/B][B][COLOR=crimson]1[/COLOR][/B][B],[/B] colx[B]))[/B]
    [COLOR=blue]Next[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Upvote 0
Hi Akuini,

It copies the cells with text in it, but does not copy the formulas from the rest of the cells below those cells. Should I include a paste special?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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