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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Re: Copy and past columns

Does this do what you're after
Code:
Sub Copycolumns()

    Dim i As Long
    Dim Header As Variant
    Dim Fnd As Range
    Header = Array("Alpha", "Beta", "Gamma", "Delta")
    
    With Sheets("Two")
        For i = 0 To UBound(Header)
            Set Fnd = .Rows(1).Find(What:=Header(i), After:=Range("A1"), _
                LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns)
            If Not Fnd Is Nothing Then
                Range(Fnd.Offset(1), Fnd.End(xlDown)).Copy Sheets("One").Range("C2").Offset(, i)
            End If
        Next i
    End With

End Sub
 
Upvote 0
Re: Copy and past columns

I'm sorry, I suppose I've not explaned that clearly.


SHEET ONE

[TABLE="width: 786"]
<tbody>[TR]
[TD][TABLE="width: 786"]
<tbody>[TR]
[TD]Ypsilon[/TD]
[TD]Alpha[/TD]
[TD]Delta[/TD]
[TD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YPSILON 10[/TD]
[TD]ALPHA 10[/TD]
[TD]DELTA 10[/TD]
[TD]BETA 10[/TD]
[TD]ICS 10[/TD]
[TD]GAMMA 10[/TD]
[/TR]
</tbody>[/TABLE]



SHEET TWO

[TABLE="width: 796"]
<tbody>[TR]
[TD]Alpha[/TD]
[TD]Beta[/TD]
[TD]Gamma[/TD]
[TD]Delta[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR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[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
Re: Copy and past columns

Try:
Code:
Sub CopyCols()

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

    With Sheets("Two")
        y = .Cells(1, .Columns.count).End(xlToLeft).column
        For x = LBound(header) To UBound(header)
            With .Cells(1, 1).Resize(, y)
                y = .find(Left$(header(x), Len(header(x)) - 2), LookIn:=xlValues).column
                LR = .Cells(.Rows.count, y).End(xlUp).row
                arr = .Cells(1, y).Resize(LR).Value
            End With
            Sheets("One").Cells(1, x + 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
            Erase arr
        Next x
    End With
        
    Erase header
    
End Sub
 
Last edited:
Upvote 0
Re: Copy and past columns

@ JackDanIce

Error 13 at the following row:

Code:
arr = .Cells(1, y).Resize(LR).Value
 
Last edited:
Upvote 0
Re: Copy and past columns

Try:
Code:
Sub CopyCols()

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

    With Sheets("Two")
        y = .Cells(1, .Columns.count).End(xlToLeft).column
        For x = LBound(header) To UBound(header)
            With .Cells(1, 1).Resize(, y)
                y = .find(header(x), LookIn:=xlValues).column
                LR = .Cells(.Rows.count, y).End(xlUp).row
                arr = .Cells(1, y).Resize(LR).Value
            End With
            Sheets("One").Cells(1, x + 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
            Erase arr
        Next x
    End With
        
    Erase header
    
End Sub
 
Last edited:
Upvote 0
Re: Copy and past columns

Try:
Code:
Sub CopyCols()

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

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


The same error at the same point.
 
Upvote 0
Re: Copy and past columns

Mocked up a copy of sheets One and Two and this seems to work:
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("Two")
        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("One").Cells(1, x + 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
            Erase arr
        Next x
    End With
        
    Erase header
    
End Sub
 
Last edited:
Upvote 0
Re: Copy and past columns

The same error again.

Mocked up a copy of sheets One and Two and this seems to work:
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("Two")
        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("One").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
Re: Copy and past columns

Unsure as I do not get the same error.

I created two sheets named One and Two and changed order of Alpha - Delta headers in Two which copied those columns to One in order of Alpha - Delta
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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