Solved

How to copy columns from one excel sheet to another based on column name

Posted on 2013-05-15
8
486 Views
Last Modified: 2013-05-19
Dear all,

I've been given a sheet with alot of data.  I want to move specific columns into new excel sheets so that i can import it into a database.

Is there any easier way than manually copying and pasting?

It would also be ideal if a string in a cell i.e. B3 of original sheet be copied into a column on the second sheet described above, with a column name that I define.

Thanks
0
Comment
Question by:AndyC1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39167366
If this is a one-time job then the easiest way is to copy then entire data over and then delete the unwanted columns.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39167459
Hi,

Whether this is a recurring task, or not, the process can be automated.

You will need to provide the complete specification of your requirements though.

Obviously, if there is a deadline, then the time taken to prepare an automated solution may be longer than actually moving the data manually (as ssaqibh mentioned).

BFN,

fp.
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 39167615
You could use the Advanced Filter function.

On the Destination sheet put only the column headers that you require.
Setup a small table with one column header and blank cell below or something that will capture all data.

In the AF function:
Data - source sheet
Criteria - area as setup above
Destination - column headers on destination sheet.
When run, only the columns for which you have headers will be copied over.

Thanks
Rob H
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Expert Comment

by:Martin Liss
ID: 39168293
Here's a macro. This one copies columns A, C and D from Sheet1 to Sheet2 but those things are easily changed.

Sub CopySomeColumns()
Dim arrCols
Dim lngIndex As Long
Dim lngCol As Long

' These are the column numbers to copy
arrCols = Array(1, 2, 4)
' This starts the pasting at column "A"
lngCol = 1

Application.ScreenUpdating = False

For lngIndex = 0 To UBound(arrCols)
    Worksheets("Sheet1").Activate
    Worksheets("Sheet1").Columns(arrCols(lngIndex)).Select
    Selection.Copy
    Worksheets("Sheet2").Activate
    Worksheets("Sheet2").Columns(lngCol).Select
    ActiveSheet.Paste
    lngCol = lngCol + 1
Next

Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:AndyC1000
ID: 39170300
Thanks Martin that's what I was after.

To extend the macro to copy i.e. columns 10, 11 and 12 into sheet 3, can this be included in the same macro? Or will I need to create another macro each time I need to copy different columns into new sheets?

I copied and adjusted the for statement for the above scenario into the same macro is this the best way?
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 39170329
Just change line 7. For example to do what you mention just change 1, 2, 4 to 10, 11, 12. You also need to change Sheet2 to Sheet3 if that's your destination sheet.
0
 

Author Comment

by:AndyC1000
ID: 39170419
Great, that worked.

Is it possible to copy column values (i.e column 2) from the third row downwards into the destination sheet?

I'm trying to avoid any manual changes to the data.  I can't delete the data above the table headers because it includes descriptors.
0
 
LVL 48

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39171316
Sure. This copies the data from row 3 downward from sheet1 to sheet3.

Sub CopySomeColumns()
Dim arrCols
Dim lngIndex As Long
Dim lngCol As Long
Dim lngLastRow As Long

lngLastRow = ActiveSheet.UsedRange.Rows.Count
' These are the column numbers to copy
arrCols = Array(10, 11, 12)
' This starts the pasting at column "A"
lngCol = 1

Application.ScreenUpdating = False

For lngIndex = 0 To UBound(arrCols)
    Worksheets("Sheet1").Activate
    Worksheets("Sheet1").Range(Cells(3, arrCols(lngIndex)), Cells(lngLastRow, arrCols(lngIndex))).Select
    Selection.Copy
    Worksheets("Sheet3").Activate
    Worksheets("Sheet3").Cells(3, lngCol).Select
    ActiveSheet.Paste
    lngCol = lngCol + 1
Next

Application.ScreenUpdating = True
End Sub

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question