Copy and paste columns

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Good morning everybody.

I've my workbook with a sheet named "One" with four columns, "Alpha", "Beta", "Gamma" and "Delta".
Another sheet is called "Two" where you can find a lot of columns among which "Alpha", "Beta", "Gamma" and "Delta" that could change their position every day.

Now, I need to copy the datas of the four columns from "Two to "One" with no considering the header (they are already set).


My attempt (at the moment I'm not able to exclude the headers)

Code:
Sub Copycolumns()

Dim header As Variant
header = Array("Alpha", "Beta", "Gamma", "Delta")
Dim i As Long 

For i = 0 To UBound(header)
Sheets("Two").Select    

On Error GoTo ErrHandler

Cells.Find(What:=Titles(i), After:=Range("A1"), _
        LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns).EntireColumn.Copy

On Error GoTo 0

    Sheets("One").Select
    Range("A1").Offset(0, i).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

NextOne:
Next i
Exit Sub

ErrHandler:
Resume NextOne

End Sub

Thank you.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Re: Copy and past columns

I wouldn't expect so with that code, I'm using 2010 to test on.
 
Upvote 0
Re: Copy and past columns

With the pc of my workmate, Excel 2016, same error.

A mystery.
 
Upvote 0
Re: Copy and past columns

Then, there's some set up I haven't been able to replicate here. You could upload a dummy file and post the link to this thread?
 
Upvote 0
Re: Copy and past columns

@Nelson78
Do all the columns you are copying have data in them?
 
Upvote 0
Re: Copy and past columns

With the help of JackDanIce, I've found the error.
Was really a banal one... simply the names of the sheets were inverted...


Code:
Sub CopyCols()

    Dim header()    As String
    Dim arr()       As Variant
    Dim x           As Long
    Dim y           As Long
    Dim LR          As Long
    Dim LC          As Long
    
    header = Split("Alpha,Beta,Gamma,Delta", ",")

    With Sheets("[COLOR=#ff0000][B]One[/B][/COLOR]")
        LC = .Cells(1, .Columns.count).End(xlToLeft).column
        For x = LBound(header) To UBound(header)
            y = .Cells(1, 1).Resize(, LC).find(header(x), LookIn:=xlValues).column
            LR = .Cells(.Rows.count, y).End(xlUp).row
            arr = .Cells(1, y).Resize(LR).Value
            Sheets("[B][COLOR=#ff0000]Two[/COLOR][/B]").Cells(1, x + 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
            Erase arr
        Next x
    End With
        
    Erase header
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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