Extract certain columns only

deduwa

Board Regular
Joined
Jul 28, 2015
Messages
110
Hi everyone.

I have a scenario where I get a dynamic (rows & columns) table with headings. All headings in the original data only appear once.

If for example there are 26 columns (A to Z), I only want 5 of these columns (Say columns with headings Apple,Bike,Cat,Drop,Elephant) from the 26 and create in a new sheet a separate table with only these 5 columns with their respective headings & data.

Note that these 5 headings can appear anywhere and not necessarily always in the same position, i.e. 'column number' with each new dynamic table coming through.

Thanks for any help.
 

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.
Additionally, in the new table, I want the columns to appear in the same order (Apple,Bike,Cat,Drop,Elephant) each time.
 
Upvote 0
Code:
Option Explicit


Sub fiveColumns()
    Dim lr As Long, lc As Long, i As Long
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    lc = s1.Cells(1, Columns.Count).End(xlToLeft).Column
    Application.ScreenUpdating = False
    For i = 1 To lc
        If s1.Cells(1, i) = "apple" Then
            s1.Cells(1, i).EntireColumn.Copy s2.Range("A1")
        ElseIf s1.Cells(1, i) = "bike" Then
            s1.Cells(1, i).EntireColumn.Copy s2.Range("B1")
        ElseIf s1.Cells(1, i) = "cat" Then
            s1.Cells(1, i).EntireColumn.Copy s2.Range("C1")
        ElseIf s1.Cells(1, i) = "drop" Then
            s1.Cells(1, i).EntireColumn.Copy s2.Range("D1")
        ElseIf s1.Cells(1, i) = "elephant" Then
            s1.Cells(1, i).EntireColumn.Copy s2.Range("E1")
        End If
    Next i
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    MsgBox "complete"


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
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